【发布时间】:2014-10-22 11:13:35
【问题描述】:
我是 Spring MVC 的新手,我有一个通常返回 org.w3c.dom.Document 对象(XML 文档)的应用程序。该文档有很多不同的(和动态的)结构(没有特定的 xsd)。我需要知道如何从我的控制器返回这些对象。例如
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Document createFOO(Document myDoc){
return myDoc;
}
当我尝试这样做时,我得到了 HTTP 406 错误,显然我需要一个配置,但我找不到解决我的问题的文档,因为在所有这些解决方案中都包括类和 XML 之间的映射,但是在在我的情况下,该对象已经是一个 XML Doc。你能给我一个指导我的调查吗?
谢谢!
马科斯
编辑:这是我的配置文件:
<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();
}
}
【问题讨论】:
-
您可能在
@RequestMapping注释中缺少produces属性。就像@RequestMapping(produces = "application/xml")一样,当然,就像你说的那样,你需要在 spring 中配置/依赖项来注册一个合适的 XML 转换器。使用它,您可以返回一个 POJO,它不像 XML 文档那样专门化。 -
谢谢。哪种转换器应该与这种回报一起使用?
-
是的,JAXB2 基础架构配置的东西少了,就是OXM……POJO 到XML 的转换类型是谁做的?
-
同理,ORM 需要 POJO 中的注解,OXM (JAXB2) 也有其他设置。
标签: java xml spring spring-mvc