简体   繁体   中英

Spring MVC 3.2: returning Document (xml) with @Responsebody

I'm new with Spring MVC and I have an application that usually returns org.w3c.dom.Document objects (XML Documents). This documents have a lot of different (and dynamic) structures (does not have a specific xsd). I need to know how can I return this objects from my controllers. eg

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Document createFOO(Document myDoc){

 return myDoc;
}

When I tried that, I got HTTP 406 error, and obviously I need a configuration, but I cannot find documentation that solves my problem, because in all of that the solution includes a mapping between a class and a XML, but in my case the object is already a XML Doc. Could you give me a direction to take in my investigation?

Thanks!

Marcos

Edit: This is my configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="my.files"/>
    <mvc:annotation-driven/>

</beans>

My example class:

@Controller
@RequestMapping("blabla")

public class MyClass{

...

    @RequestMapping(method = RequestMethod.POST, produces = "application/xml")
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public Document myMethod(...) {

        Document responseDoc = foo.giveMeaDocument();
    }
}

The HTTP/406 error is due to the fact that the document object can't be mapped to the response body.

You will need to make sure you are using <mvc:annotation-driven /> in your XML configuration or the similar construct under JavaConfig style.

EDIT

I have implemented a quick and VERY dirty application to demonstrate what I am talking about:

https://github.com/djgraff209/domconversion

See if that meets your needs - be advised it is VERY rough and is only to demonstrate the technology.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM