我是 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 配置中使用
编辑
我已经实现了一个快速且非常脏的应用程序来演示我正在谈论的内容:
https://github.com/djgraff209/domconversion
看看这是否满足您的需求 - 请注意,它非常粗糙,只是为了演示技术。
关于java - Spring MVC 3.2 : returning Document (xml) with @Responsebody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25551374/