0

I have a Spring @RestController for manipulating my Users and I want to have several functions:

  • /users : GET (returns all users)
  • /users/:id : GET (returns a user with given ID, default id=1)
  • /users : POST (inserts a user)
  • /users/:id : DELETE (deletes a user with given ID)

I started working on it but I'm not sure how to manage the "overlapping" URIs for the same method (e.g. first two cases). Here's what I came with so far:

@RestController
public class UserController {
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<User> getAllUsers() {
        return UserDAO.getAll();
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public User getUser(@RequestParam(value = "id", defaultValue = "1") int id) {
        return UserDAO.getById(id);
    }
}

This won't work due to "ambiguous mapping" and it's pretty clear to me, but I don't know what to do. Should I change one of the URIs or there is some other way?

Edit: I've also tried changing the second method to:

@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable("id") int id) {
    return UserDAO.getById(id);
}

Still doesn't work.

6
  • 3
    If you want to map the desired /users/:id , you should define a @RequestMapping(value = "/users/{id}", method = RequestMethod.GET) and capture that {id} with a @PathVariable int id. Commented Nov 29, 2016 at 8:26
  • @AliDehghani look at my edit... it still doesn't work Commented Nov 29, 2016 at 8:27
  • 1
    What do you mean by Still doesn't work..? If getting any exception, post the full stacktrace... Commented Nov 29, 2016 at 8:27
  • @AliDehghani java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'candidateController' method public java.util.List<model.Candidate> controller.CandidateController.getAllCandidates() to {[/candidates],methods=[GET]}: There is already 'candidateController' bean method public model.Candidate controller.CandidateController.getUser(int) mapped. Commented Nov 29, 2016 at 8:27
  • 1
    My mistake... I have changed one file but the error was in another one. I'm a moron :). @AliDehghani if you want to post an answer, I will accept it. Commented Nov 29, 2016 at 8:33

1 Answer 1

3

Your current mapping:

@RequestMapping(value = "/users", method = RequestMethod.GET)
public User getUser(@RequestParam(defaultValue = "1") int id)

Would map to the /users?id=42 not the desired /users/42. If you want to create a mapping for /users/:id endpoint, use the following:

@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable int id) {
    return UserDAO.getById(id);
}

Also, as of Spring Framework 4.3, you can use new meta annotations to handle GET, POST, etc. methods:

@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping
    public List<User> getAllUsers() {
        return UserDAO.getAll();
    }

    @GetMapping("{id}")
    public User getUser(@PathVariable int id) {
        return UserDAO.getById(id);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@sura2k Then this may sound interesting, too.

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.