@Repository
public interface UserJpaRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USER WHERE EMAIL = ?", nativeQuery = true)
Optional<User> findByEmail(String email);
}
}
@Override
public User getByEmail(String email) {
Optional<User> userDB = repo.findByEmail(email);
if (userDB.isPresent()) {
return userDB.get();
}
else {
throw new ResourceNotFoundException("record not found with this id: " + email);
}
}
@GetMapping("/get/{email}")
public User showByEmail(@RequestParam(value = "email") String email) {
return userService.getByEmail(email);
}
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Ambiguous handler methods mapped for '/get/snvairagi003@gmail.com': {public com.example.model.User com.example.controller.UserController.showById(java.lang.Long), public com.example.model.User com.example.controller.UserController.showByEmail(java.lang.String)}] with root cause
java.lang.IllegalStateException: Ambiguous handler methods mapped for '/get/snv': {public com.example.model.User com.example.controller.UserController.showById(java.lang.Long), public com.example.model.User com.example.controller.UserController.showByEmail(java.lang.String)}
答案 0 :(得分:1)
当您有多个方法映射到同一端点时,会发生这种情况。
确保您拥有具有一个端点和方法签名的确切一种方法。
通过查看代码,您尝试将参数作为URL的一部分发送,而URL实际上是路径变量,而不是请求参数。
如果您想接受电子邮件作为路径变量。
更改
@RequestParam(value = "email") String email
到
@PathVariable("email") String email
如果要接受为请求参数,请从如下所示的网址中删除{email}。
@GetMapping("/get/")