Reputation: 2053
I want to know if it's possible to map the same URL to different methods in the RestController
class, based only in the request body. For example:
@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody String id) {
//do something
}
@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody Book book) {
//do something
}
The request body will always be a JSON payload. if it's {"id":"foo"}
I want the first method to be called. If the request body is:
{
"title":"Spring Guide",
"author":"John Doe"
}
I want the second method to be called. Is this possible?
Upvotes: 7
Views: 7125
Reputation: 2053
There is no way to differentiate only by payload.
Based on the tests I did here and M. Deinum and Ali Dehghani's response I think the best way to do this is to have different urls for each case. So a /books/{id}
to delete by the id, and a /books
with the object's JSON in the body to delete passing the object. Thanks for all that commented.
Upvotes: 5