1

Repository

@Repository
public interface UserJpaRepository extends JpaRepository<User, Long> {
    
    @Query(value = "SELECT * FROM USER WHERE EMAIL = ?", nativeQuery = true)
    Optional<User> findByEmail(String email);
    }
}

Service

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

Controller

@GetMapping("/get/{email}")
public User showByEmail(@RequestParam(value = "email") String email) {
    return userService.getByEmail(email);
}

Error

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)}
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
Vinay Bairagi
  • 331
  • 1
  • 3
  • 8
  • Looks like the UserController has one more request mapping '/get/{id}' for showById(java.lang.Long). The request mapping should be unique. Refer - https://stackoverflow.com/questions/35155916/handling-ambiguous-handler-methods-mapped-in-rest-application-with-spring – Balaji Oct 07 '20 at 18:18

1 Answers1

3

This happens when you have multiple method mapped with same endpoint.

Ensure that you have exact one method with one endpoint and method signature.

By seeing your code, you are trying to send parameters as part of URL which is path variable actually instead of request parameter.

If you want to accept email as a path variable.

Change

@RequestParam(value = "email") String email 

to

@PathVariable("email") String email

And if want to accept as request parameter then remove {email} from url like below.

@GetMapping("/get/")
Alien
  • 15,141
  • 6
  • 37
  • 57