How to configure an endpoint in Spring to accept both form data and XML request body?
I have a small question regarding Spring's MVC data binding capabilities. I do have the following controller class:
@Controller
@RequestMapping("/foo")
public class FooController() {
// … some init stuff //
@RequestMapping(value = "/{id}/edit.{format}", method = RequestMethod.POST)
public ModelAndView editFoo(@RequestBody FooItem foo, @PathVariable("format") String format) {
// some code here to edit the FooItem //
}
}
I want to be able to post form data as well as XML against this method. 开发者_如何学CFor that to work I added two message converters to my applicationContext.xml
: The default formHttpMessageConverter
and an XStream marshaller.
This works fine, but I have a problem, that if I use @RequestBody
and post form data against the URL, the server responds with a 415 Error. If I remove this annotation, form data works well and Spring creates the object for me, but if I post XML against it, I get an empty object.
Is there any way around this or do I need to have 2 methods to be able to handle both of the incoming formats?
Thanks in advance!
I think you need two methods.
FormHttpMessageConverter
doesn't have the same databinding capabilities as @ModelAttribute
provides, it can't bind request to the specified target class, only to MultiValueMap
(see javadoc).
精彩评论