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.
/users/:id, you should define a@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)and capture that{id}with a@PathVariable int id.Still doesn't work..? If getting any exception, post the full stacktrace...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.