Does it has built-in support or I have to do java objects to xml converting by myself? An example would be very helpful. Thanks!
1 Answer
Assuming that you're using Spring 3, then you can have it automatically marshall the response to XML:
@RequestMapping("/someurl")
public @ResponseBody SomeObject someMethod() {
...
return instanceOfSomeObject;
}
Then in your context you would register an instance of HttpMessageConverter that supports XML, such as MarshallingHttpMessageConverter (with an appropriate Marshaller configured).
3 Comments
Bobo
Can you please elaborate this part by an example " register an instance of HttpMessageConverter that supports XML, such as MarshallingHttpMessageConverter (with an appropriate Marshaller configured)"?
GaryF
@Bobo Spring has an interface, HttpMessageConverter, that allows objects to be automatically converted to other formats when requested over http. For XML, one instance of this is the MarshallingHttpMessageConverter. It requires an implementation of Marshaller to work, such as Castor. So, if you set-up Castor and a MarshallingHttpMessageConverter in your spring context, that should be enough that if someone asks for text/xml it'll return XML.
Bobo
I dont think I set up anything for that but it works as long as there is proper JAXB annotation for the Java Bean.