24

Pardon me for posting this noob question, but I have been debugging this problem for quite awhile now. I'm having a little problem trying to get the response to return the XML based on the object:-

@RequestMapping(value = "/mylink", method = RequestMethod.GET)
public @ResponseBody SomeObject doIt() {
    ...
}

Right now, even though that API is called, my client side does not receive the XML response at all. I have been reading a few places and it seems like I need to configure the XML marshaller or somesort of XML resolvers, but I'm not sure how to integrate that piece into my existing configuration. I currently have the following configuration in my servlet.xml:-

<context:component-scan base-package="ss.controller" />

<mvc:annotation-driven />

<mvc:resources location="/resources/" mapping="/resources/**" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/app/" />
    <property name="suffix" value=".jsp" />
</bean>

Can someone kindly post some sample configurations on how I might go about in configuring my servlet.xml to get this working? Thanks much.

6 Answers 6

22

This can be done by adding the following bit of magic to the Spring context (see docs):

<mvc:annotation-driven/>

which amongst other things, provides:

Support for reading and writing XML, if JAXB is present on the classpath.

If JAXB is detected (i.e. if you're on Java6, or otherwise have some JAXB implementation on your classpath), this will register a Jaxb2RootElementHttpMessageConverter with the context, and will provide the ability to spit out XML from the return value of the @ResponseBody-annotated method.

Note: Your question sort-of suggested using a ViewResolver for rendering the XML, but this isn't necessary. The @ResponseBody annotation is designed to bypass the view layer altogether.

6
  • Agreed about the bloat. Spring needs to have an out of the box solution for this. It's ridiculous. Jan 31, 2011 at 22:40
  • @ericacm: I had a re-think about this and went and looked at the source code (and then re-read the documentation), and it's much easier than I first thought. See edit.
    – skaffman
    Jan 31, 2011 at 22:50
  • I have jaxb-api-xx.jar in the classpath, but it doesn't seem to be doing anything. Am I supposed to annotate the SomeObject class properties with JAXB's annotations for this to work?
    – limc
    Feb 1, 2011 at 1:05
  • 6
    In addition to adding jaxb-api.jar and jaxb-impl.jar (I'm using Java5), I have to annotate SomeObject with @XmlRootElement... everything works like a charm then. Thanks for your help!
    – limc
    Feb 1, 2011 at 1:22
  • 1
    Incase if I return the list of Object it automatically converts to json. How can I force to use xml always? (Also posted in tinyurl.com/3qqycu9)
    – kalyan
    Sep 7, 2011 at 13:44
17

I solved this problem with Spring 3 mvc without MarshallingView

@RequestMapping(value = "actionName.xml", method = RequestMethod.GET)
public HttpEntity<byte[]> getXml(ModelMap map, HttpServletResponse response) {

    String xml = generateSomeXml();

    byte[] documentBody = xml.getBytes();

    HttpHeaders header = new HttpHeaders();
    header.setContentType(new MediaType("application", "xml"));
    header.setContentLength(documentBody.length);
    return new HttpEntity<byte[]>(documentBody, header);
}

that's all. greetings

7

What I do when I want to return an XML representation of objects using spring is that I define a MarshallingView, e.g.,

<!-- XML view using a JAXB marshaller -->
<bean id="jaxbView" class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
        <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.company.AClass</value>
                </list>
            </property>
        </bean>
    </constructor-arg>
</bean>

<!-- Resolve views based on string names -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

Note that there is a whole world of alternatives to jaxb. The next step is

@RequestMapping("/request")
public ModelAndView sample() {
    return new ModelAndView("jaxbView", "data", "data_to_be_turned_into_xml");
}

Or if you want to use the ResponseBody annotation, it would look like:

@RequestMapping("/request")
@ResponseBody
public void sample() {
    return "data_to_be_turned_into_xml"
}

Note that this requires defining a HttpMessageConverter. See the spring documentation for a perfect sample on how to do this.

1
  • Is it possible to make a method that returns either a "viewname" or a @ResponseBody?
    – trusktr
    Feb 25, 2014 at 5:18
5

You will probably either have to use an XML Marshalling View or configure a MarshallingHttpMessageConverter.

Here's a short Reference about using @ResponseBody with converters.

2

Try adding produces = MediaType.APPLICATION_XML_VALUE, i.e.

@RequestMapping(value = "/mylink", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
1

Adding produces = MediaType.APPLICATION_XML_VALUE to the RequestMapping and @XmlRootElement to the top of your model object should works

@RequestMapping(value = "/mylink", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public SomeObject doIt(){
    return new SomeObject();
}

@XmlRootElement
public class SomeObject {

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.