0

so I am creating a User API and I'm trying to call getUserByEmail() before login. My problem is that I get a Ambiguous handler methods mapped for HTTP path ERROR.

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user/{email}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUserByEmail(@PathVariable("email") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

Here is the error response I get:

{"timestamp":1460226184275,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalStateException","message":"Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/user/fr': {public org .springframework.http.ResponseEntity com.ffa.controllers.UserController.getUser(int), public org.springframework .http.ResponseEntity com.ffa.controllers.UserController.getUserByEmail(java.lang.String)}","path":"/user /fr"}

I know my problem has something to do with me having a GET that is the same but with different parameter types. Any help would be greatly appreciated!

5
  • 1
    Combine your mapping methods and have logic in place which will fetch use depending upon your id or email address Commented Apr 9, 2016 at 18:36
  • 1
    Currently as per your logic /user/ does not know if it's an email for id. Either have /user/ email ? Email= and /user/id?id=id Commented Apr 9, 2016 at 18:37
  • @LearningPhase I want them to be separate, since each one does something different. How do I modify this so that it knows which one to use? Commented Apr 9, 2016 at 18:41
  • 1
    Use /user/id/{id} and /user/email/{email} Commented Apr 9, 2016 at 18:44
  • Use something like /user/id/{Id} and /user/email/{email} Commented Apr 9, 2016 at 18:48

1 Answer 1

4

Instead of PathVariables you can use RequestParams with two different URLs.

@RequestMapping(value = "/user", params="userID", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@RequestParam("userID") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user", params="emailID, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE")
public ResponseEntity<User> getUserByEmail(@RequestParam("emailID") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

When you call /user?userID=123 it will invoke the first one and when you call /[email protected] it will invoke the second one. This is taken care by the params property which is passed in the @RequestMapping.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.