简体   繁体   English

Spring MVC 3.2:使用@Responsebody返回文档(xml)

[英]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). 我是Spring MVC的新手,我有一个通常返回org.w3c.dom.Document对象(XML文档)的应用程序。 This documents have a lot of different (and dynamic) structures (does not have a specific xsd). 该文档具有许多不同的(动态的)结构(没有特定的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. 当我尝试这样做时,出现HTTP 406错误,显然我需要配置,但是我找不到解决我问题的文档,因为所有这些解决方案都包括类和XML之间的映射,但就我而言,对象已经是XML文档。 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. HTTP / 406错误是由于无法将文档对象映射到响应主体而造成的。

You will need to make sure you are using <mvc:annotation-driven /> in your XML configuration or the similar construct under JavaConfig style. 您将需要确保在XML配置或JavaConfig样式下的类似构造中使用<mvc:annotation-driven />。

EDIT 编辑

I have implemented a quick and VERY dirty application to demonstrate what I am talking about: 我已经实现了一个快速且非常肮脏的应用程序,以演示我在说什么:

https://github.com/djgraff209/domconversion https://github.com/djgraff209/domconversion

See if that meets your needs - be advised it is VERY rough and is only to demonstrate the technology. 看看是否满足您的需求-告知它非常粗糙,仅用于演示技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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