Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

java - Spring MVC 3: Returning XML through @ResponseBody

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...