java.lang.IllegalStateException:映射的模糊处理程序方法

时间:2020-10-07 18:10:09

标签: spring-boot

存储库

@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

500 internal server error

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

1 个答案:

答案 0 :(得分:1)

当您有多个方法映射到同一端点时,会发生这种情况。

确保您拥有具有一个端点和方法签名的确切一种方法。

通过查看代码,您尝试将参数作为URL的一部分发送,而URL实际上是路径变量,而不是请求参数。

如果您想接受电子邮件作为路径变量。

更改

@RequestParam(value = "email") String email 

@PathVariable("email") String email

如果要接受为请求参数,请从如下所示的网址中删除{email}。

@GetMapping("/get/")
相关问题