aspose file tools*
The moose likes Spring and the fly likes Rendering XML as a response of @RequestMapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Rendering XML as a response of @RequestMapping " Watch "Rendering XML as a response of @RequestMapping " New topic
Author

Rendering XML as a response of @RequestMapping

Swati Sisodia
Greenhorn

Joined: Oct 28, 2007
Posts: 19
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Swati Sisodia
Greenhorn

Joined: Oct 28, 2007
Posts: 19
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.
megha Ranjan
Greenhorn

Joined: Jul 28, 2011
Posts: 10

Swati Sisodia wrote:
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:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FilesResponse {
private List<File> fileList= new ArrayList<File>();

@XmlElement
@XmlElementWrapper(name = "files")
public List<File> getFiles() {
return fileList;
}

public void setOrders(List<File> fileList) {
this.fileList= fileList;
}
}


Here,@XmlRootElement will create the root element as <FilesResponse>.
@XmlElemenentWrapper is required for list elements.

Next, Your File class will look like:

import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;

@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);

where response is instance of FilesResponse.


Thanks,

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rendering XML as a response of @RequestMapping
 
Similar Threads
@Responsebody for HTTP msg and a View to show the HTTP response msg
Spring 3 MVC: No ViewResolver found with url-pattern /*
Spring Mvc 404 error
Spring Consume xml ?
Spring MVC Request Mapping