首页 > 解决方案 > "message": "从类型 [java.lang.String] 转换为类型 [java.lang.Long] 的值 'count' 失败;对于输入字符串:\"count\""

问题描述

我需要使用spring boot在mysql中计算用户来显示统计信息

我的代码内容 3 类和接口:

////////////////interface////////////////////
public interface UserRepository extends JpaRepository<User, Long>

String count(String name);

////////////////service////////////////////
public String count(String username);

@Override
public String count(String username) {
    return userRepository.count(username);
}

//////////////////iontroller////////////
@GetMapping("/count")
public String count(String username) {
    return userService.count(username);
}

结果是邮递员:

"message": "Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'count'; nested exception is java.lang.NumberFormatException: For input string: \"count\""

标签: java

解决方案


在存储库中,您需要使用countBy带有搜索字段名称的子句并返回 long 作为返回类型。假设User实体username是一个字段。

long countByUsername(String username);

推荐阅读