Reputation: 913
I would like to achieve something like this with Spring MVC
@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
@RequestMapping(value = "/user/{userId}/delete", method = RequestMethod.POST)
public void deleteUser(@PathVariable String userId) {
...
}
This would give me a common endpoint for REST calls and standard HTML form posts. Is it possible to do with Spring MVC? All I can come up with is
@RequestMapping(value = { "/user/{userId}", "/user/{userId}/delete"}, method = {RequestMethod.DELETE, RequestMethod.POST})
public void deleteUser(@PathVariable String userId) {
...
}
but the result is slightly different because a POST to "/user/{userId}" would also delete the user.
Upvotes: 1
Views: 2059
Reputation: 1145
Sorry, got it wrong way around.
In mature REST architecture, code should use a URL to refer to a resource and use HTTP method to define the action on the resource. So just define a @RequestMapping("/user/{userId}/delete", method = RequestMethod.DELETE)
and eliminate the POST. See DELETE vs POST.
Upvotes: 4
Reputation: 657
One thing you could do is make 2 separate methods with their own RequestMapping
annotation, and then just pass the parameters on to a different method, where you do actual stuff:
@RequestMapping(value = "/user/{userId}/delete", method = RequestMethod.POST)
public void deleteUserPost(@PathVariable String userId) {
deleteUser(userId);
}
@RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
public void deleteUserDelete(@PathVariable String userId) {
deleteUser(userId);
}
private void deleteUser(String userId){
//Do things here
}
Upvotes: 5