Hello,
I am working on a Spring application and I am new to Spring (or any framework). So, pardon me if I sound too naive.
My method annotated with @RequestMapping is responsible for storing the image(s) on server and return back the id generated in XML format. So, the method is got to be POST. I need help in generating the response in XML format. Below is the code I have:
I need the response in XML format:
<files>
<file>
<fname>abc.jpg</fname>
<id>123</id>
</file>
<file>
<fname>xyz.jpg</fname>
<id>456</id>
</file>
</files>
If there are Spring config entries, please tell me that too.
Which version of Spring are you using? Are you using any OXM Object to XML mapping frameworks like JAXB?
If you are using Spring 3.0 you can just add a few annotations and make it a REST Webservice. And have the client request the return value to be xml.
Or if you are using JAXB and not Spring 3.0, then you can use the JAXB classes to generate xml from a domain object annotated with JAXB annotation mappings.
As far as your code. you do not have to return a ModelAndView object. You can make your method any signature that you like.
Mark Spritzler wrote:Which version of Spring are you using? Are you using any OXM Object to XML mapping frameworks like JAXB?
Sorry for not mentioning that earlier. I am using Spring 3.0.5 and JAXB 2.0.
Mark Spritzler wrote:If you are using Spring 3.0 you can just add a few annotations and make it a REST Webservice. And have the client request the return value to be xml.
That's precisely what I would want to do.
I also got the feeling that I do not need to return ModelAndView, but was unable to work out the details. Can you please explain the annotations I need to add to return the XML in a little detail. Thanks much.
Mark Spritzler wrote:Which version of Spring are you using? Are you using any OXM Object to XML mapping frameworks like JAXB?
Sorry for not mentioning that earlier. I am using Spring 3.0.5 and JAXB 2.0.
Mark Spritzler wrote:If you are using Spring 3.0 you can just add a few annotations and make it a REST Webservice. And have the client request the return value to be xml.
That's precisely what I would want to do.
I also got the feeling that I do not need to return ModelAndView, but was unable to work out the details. Can you please explain the annotations I need to add to return the XML in a little detail. Thanks much.
Hi Swati,
If you are using spring3.0 and jaxb its pretty simple to do this. You need to first use some annotations in your response.Like shown below:
@XmlType(propOrder = { "fname","id" } )
@XmlAccessorType(XmlAccessType.FIELD)
public class File{
@XmlElement
private String fname;
@XmlElement
private long id;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id= id;
}
}
In this code @XmlAccessorType(XmlAccessType.FIELD) to allow me to place the @XmlElement annotations on the private fields instead of on the getter methods.
Also notice the propOrder parameter of the @XmlType annotation. By default JAXB will order the elements alphabetically. Use the propOrder parameter to specify the order when marshaling to XML.
This should work fine. Also you need to return the ModelViewController on controller class as
return new ModelAndView("response","response",response);