简体   繁体   中英

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

I need count users in mysql using spring boot to display a statistic

my code content 3 class and interface :

////////////////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);
}

result in postman:

"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\""

In the repository you need to use countBy clause with searching field name and return long as return type. Assuming in User entity username is a field.

long countByUsername(String username);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM