Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping the same url to different methods based on request body in spring

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?

like image 435
franksands Avatar asked Apr 07 '16 17:04

franksands


1 Answers

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.

like image 163
franksands Avatar answered Oct 21 '22 05:10

franksands