Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC 3.2:使用@ResponseBy返回文档(xml)_Java_Xml_Spring_Spring Mvc - Fatal编程技术网

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

Java Spring MVC 3.2:使用@ResponseBy返回文档(xml),java,xml,spring,spring-mvc,Java,Xml,Spring,Spring Mvc,我是SpringMVC新手,我有一个通常返回org.w3c.dom.Document对象(XML文档)的应用程序。这些文档有很多不同的(动态)结构(没有特定的xsd)。我需要知道如何从控制器返回这些对象。e、 g @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) @ResponseBody public Document createFOO(Document myDoc){ ret

我是SpringMVC新手,我有一个通常返回org.w3c.dom.Document对象(XML文档)的应用程序。这些文档有很多不同的(动态)结构(没有特定的xsd)。我需要知道如何从控制器返回这些对象。e、 g

@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>

HTTP/406错误是由于文档对象无法映射到响应体

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

编辑

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


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

您可能缺少
注释中的
@RequestMapping
属性。就像这个
@RequestMapping(products=“application/xml”)
一样,当然,正如您所说,您需要在spring中配置/依赖项来注册适当的xml转换器。使用它,您可以返回一个POJO,而不是像XML文档那样专门化的文件。谢谢。哪种转换器应该使用这种返回?是的,缺少关于JAXB2基础结构配置的信息,是OXM…谁进行从POJO到XML的转换类型?同样,ORM需要POJO中的注释,OXM(JAXB2)也有其他设置。感谢您的回答:是的,我的组件扫描与mvc不同:注释驱动。注释驱动程序使用@RequestBody/@ResponseBody注释设置处理程序来处理JSON、XML等。所以我不知道。我想既然我成功地将@ResponseBody与字符串一起使用(在其他控制器中),问题就不存在了。此外,我是否需要xml转换器?如果是:为什么?因为文档对象是XML。非常感谢您的回复!如果您添加注释驱动,那么在使用JVM默认处理时,应该会自动将其绑定在一起,我尝试过,但问题尚未解决。我的代码如下:@RequestMapping(method=RequestMethod.POST,products=“application/xml”)@ResponseStatus(HttpStatus.CREATED)@ResponseBody公共文档createFOO(…){…return aDocument;}但错误406仍然存在。
@Controller
@RequestMapping("blabla")

public class MyClass{

...

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

        Document responseDoc = foo.giveMeaDocument();
    }
}