I am going to implement a RESTful web service using Spring. Let it be a regular PUT method, something like this:
@RequestMapping(method=RequestMethod.PUT, value="/foo") public @ResponseBody void updateFoo(@RequestBody Foo foo) { fooService.update(foo); }
In this case, the input JSON format (if it corresponds to the Foo class) will be successfully converted to the Foo instance without any additional efforts, or an error will be generated if the format is incorrect. But I would like the service to be able to consume two different types of formats using the same method (e.g. PUT) and the same URL (e.g. / foo).
To make it look like this:
//PUT method #1 @RequestMapping(method=RequestMethod.PUT, value="/foo") public @ResponseBody void updateFoo(@RequestBody Foo foo) { fooService.update(foo); } //PUT method #2 @RequestMapping(method=RequestMethod.PUT, value="/foo") public @ResponseBody void updateFoo(@RequestBody FooExtra fooExtra) { fooService.update(fooExtra); }
Converter
and Spring tried to convert the input JSON not only to Foo, but also to FooExtra, and also called the appropriate PUT method depending on the input format.
In fact, I tried to implement it exactly as described above, but to no avail. Is it possible? Maybe I need some kind of "trick"? What is the best (and most correct) way to achieve this behavior? Of course, I could always create two different URLs, but I would like to know if this is possible with the same thing.
source share