I’ve got the following code I’m just doing on a PoC with SpringMVC…
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
@ResponseBody
public User data(Locale locale, Model model, User user) {
logger.info("getUser controller loaded...");
return userService.getById(user.getId());
}
@RequestMapping(value = "/setUser", method = RequestMethod.GET)
@ResponseBody
public void data(Locale locale, Model model, User user) {
logger.info("setUser controller loaded...");
userService.addUser(user);
}
As you can see, this creates a problem as both method signatures are the same. What’s the best way to split these up as the annotations seem to be deriving the functionality rather than the method names. Can the method names be anything? What’s the best practice way in SpringMVC to do setter and getter controllers?
Not really a best practice, but this is what I have used:
method name for gets – either
list(for a list) orshow(for an item)method name for sets –
updateorcreatebased on whether you are updating an item or creating a new itemThe method names can be anything, it is the
@RequestMappingannotation which ensures that the right method is called, having REST based mapped methods is considered a good practice though – consider say a domain like user, a RESTFUL controller would look like this: