H2 TABLE: USERS
| USER_ID | USER_NAME | USER_PASSWORD | USER_RIGHTS |
-----------------------------------------------------
| 1 | rooter | 123456 | ROOT |
@Entity
@Getter
@Setter
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;
@Column(name = "user_name")
private String userName;
@Column(name = "user_password")
private String userPassword;
@Column(name = "user_rights")
private String userRights;
// equals() and toString()
}
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserName(String userName);
User findByUserId(Long id);
}
@RestController
@RequestMapping("/user")
public class UsersApi {
private final UserRepository userRepository;
public UsersApi(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/test")
public List<User> test(String name, Long id) {
List<User> response = new LinkedList<>();
response.add(userRepository.findByUserName(name));
response.add(userRepository.findByUserId(id));
return response;
}
}
When I visit /user/test?name=rooter&id=1
it will response with:
[
null,
{
"userId":1,
"userName":"root",
"userPassword":"123456",
"userRights":"ROOTER"
}
]
Why the method findByUserName(String userName)
cannot find the data?
In the response array the username of the user with id 1 is root and the provided request parameter for finding a user by its username is rooter. So I would recommend you to check if the userName is root instead of rooter.
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.