I'm going to implement a RESTful webservice using Spring. Let it be an ordinary PUT method, something like this:
@RequestMapping(method=RequestMethod.PUT, value="/foo")
public @ResponseBody void updateFoo(@RequestBody Foo foo) {
fooService.update(foo);
}
In such a case input JSON format (if it corresponds to Foo class) will be successfully converted to Foo instance with no extra efforts, or error will be issued in case of wrong format. But I'd like to make the service able to consume two different types of formats using same method (e.g. PUT) and same URL (e.g. /foo).
So that it possibly looked like:
//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);
}
and Spring converter tried to convert input JSON not only in Foo but in FooExtra as well and invoked corresponding PUT method depending on input format.
In fact, I tried to implement it exactly as it described above but without success. Is it even possible? Maybe, I need some kind of "trick"? What is the best (and the most proper) way to achieve such behavior? Of course, I could always make two different URLs but I'd like to know whether it is possible with the same one.
RESTfulis then you also know about media types. That's the way to go.