• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Rendering XML as a response of @RequestMapping

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Swati Sisodia
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 10
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic