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

标签 java xml spring spring-mvc

我是 Spring MVC 新手,我有一个通常返回 org.w3c.dom.Document 对象(XML 文档)的应用程序。该文档有很多不同的(和动态的)结构(没有特定的 xsd)。我需要知道如何从我的 Controller 返回这些对象。例如

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

 return myDoc;
}

当我尝试这样做时,我收到了 HTTP 406 错误,显然我需要一个配置,但我找不到解决我的问题的文档,因为所有这些解决方案都包含类和 XML 之间的映射,但在我的例子中,对象已经是 XML 文档。您能给我一个调查方向吗?

谢谢!

马科斯

编辑:这是我的配置文件:

<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>
<小时/>

我的示例类:

@Controller
@RequestMapping("blabla")

public class MyClass{

...

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

        Document responseDoc = foo.giveMeaDocument();
    }
}

最佳答案

HTTP/406错误是由于文档对象无法映射到响应正文造成的。

您需要确保在 XML 配置中使用 或 JavaConfig 样式下的类似构造。

编辑

我已经实现了一个快速且非常脏的应用程序来演示我正在谈论的内容:

https://github.com/djgraff209/domconversion

看看这是否满足您的需求 - 请注意,它非常粗糙,只是为了演示技术。

关于java - Spring MVC 3.2 : returning Document (xml) with @Responsebody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25551374/

相关文章:

xml - 如何用 XML 编写路径文件?

spring - Websockets - 带有 Spring Boot 和 STOMP 的 CSRF

java - 如何在 Java 中对 ArrayList 中的对象中的元素进行排序

java - 在一组元素中查找复杂元素

javascript - 将 XPath 表达式与 javascript 变量进行匹配

php - 使用 PHP 解析 XML - 其中包括 & 符号和其他字符

使用 spring @Transactional 的 Spring Boot 无需启用事务管理即可工作

java - spring mvc 是否有 response.write 可以直接输出到浏览器?

java - 如何根据多个标准对多重图进行索引?

java - 如何在单独的文件中创建命名查询