尝试为Spring Web MVC服务配置XML编组传输时出现“406不可接受”错误。

3
一个能够展现我所遇到问题的玩具例子可以在github上找到。简而言之,我有一个需要通过HTTP返回XML的服务。我正在尝试使用Spring Web MVC来设置它。需要进行编组回调的响应对象是从模式生成的JAXB2(因此缺少@XmlRootElement标记,但是生成它的包中有一个ObjectFactory,提供了生成JAXBElement-s的方法,使XML编组器满意)。我根据Google搜索尝试了不同的spring上下文配置,这些大部分都是在stackoverflow上发布的帖子,但是我无法让它们中的任何一个对我有用。

环境:

  • Spring 4.0.5。
  • Tomcat 7.0.5X。

这里有一个请求/响应循环示例,展示了问题(省略了部分输出):

$ curl -v -X GET -H "Accept: application/xml" http://localhost:8080/sotaro/say/boo
* Connected to localhost (::1) port 8080 (#0)
> GET /sotaro/say/boo HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: application/xml
>
< HTTP/1.1 406 Not Acceptable
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 1067
< Date: Wed, 04 Jun 2014 12:48:44 GMT
<
<html>
  <body>
    <h1>HTTP Status 406 -</h1>
    <p><b>message</b></p>
    <p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p>
  </body>
</html>

这是一个玩具样例的模块。
我尝试过的上下文配置之一:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util" 
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:oxm="http://www.springframework.org/schema/oxm"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd">

  <context:component-scan base-package="io.github.gv0tch0.sotaro"/>

  <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="ignoreAcceptHeader" value="false" />
    <property name="useJaf" value="false" />
    <property name="defaultContentType" value="application/xml" />
    <property name="mediaTypes">
      <map>
        <entry key="xml" value="application/xml" />
      </map>
    </property>
  </bean>

  <bean id="xmlConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="jaxbMarshaller" />
    <property name="supportedMediaTypes" value="application/xml" />
  </bean>

  <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="packagesToScan">
      <list>
        <value>io.github.gv0tch0.sotaro.*</value>
      </list>
    </property>
  </bean>

  <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters register-defaults="false">
      <ref bean="xmlConverter" />
    </mvc:message-converters>
  </mvc:annotation-driven>

</beans>

控制器:

@Controller
public class Say {
  @RequestMapping(value = "/say/{what}", 
                  produces = {MediaType.APPLICATION_XML_VALUE}, 
                  method = RequestMethod.GET)
  public @ResponseBody SayWhat say(@PathVariable("what") String what) {
    return echo(what);
  }
  private SayWhat echo(String what) {
    SayWhat echo = new SayWhat();
    echo.setWhat(what);
    return echo;
  }
}

响应对象(由JAXB2生成):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SayWhat", propOrder = {"what"})
public class SayWhat {
  @XmlElement(required = true)
  protected String what;

  public String getWhat() {
      return what;
  }
  public void setWhat(String value) {
    this.what = value;
  }
}

生成的模式如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="urn:io:github:gv0tch0:sotaro"
           targetNamespace="urn:io:github:gv0tch0:sotaro"
           version="0.0.1">
  <xs:element name="say" type="tns:SayWhat" />
  <xs:complexType name="SayWhat">
    <xs:sequence>
      <xs:element name="what" type="xs:string" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

你尝试过返回 JAXBElement<SayWhat> 吗? - M. Deinum
我刚刚做了。没有解决问题。同时,我在 github repo 上创建了一个包含更改的分支。 - gv0tch0
通常情况下,阅读一些代码是无可替代的。因此,如果要支持编组JAXBElement,则需要将Jaxb2MarshallersupportJaxbElementClass属性配置为true。一旦这样做了,并且响应对象当然被包装在JAXBElement中,那么世界上就一切都好了。 - gv0tch0
哇..这里在堆栈上的评论编辑规则..有点奇怪..无论如何,接着上一个评论:代码还揭示了如果响应没有被JAXBElement包装,它需要被JAXB注释并具有@XmlRootElement注释。除非编组实现(例如EclipseLink MOXy)支持并提供外部绑定源。 - gv0tch0
1个回答

2

罪魁祸首是响应对象缺少JAXBElement包装器和配置为支持JAXBElement包装响应的Jaxb2Marshaller

@Controller变成:

@Controller
public class Say {
  private final static ObjectFactory JAXB_FACTORY = new ObjectFactory();

  @RequestMapping(value = "/say/{what}", 
                  produces = {MediaType.APPLICATION_XML_VALUE}, 
                  method = RequestMethod.GET)
  public @ResponseBody JAXBElement<SayWhat> say(@PathVariable("what") String what) {
    return echo(what);
  }

  private JAXBElement<SayWhat> echo(String what) {
    SayWhat echo = new SayWhat();
    echo.setWhat(what);
    return JAXB_FACTORY.createSay(echo);
  }
}

而spring配置中的Jaxb2Marshaller部分变为:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="supportJaxbElementClass" value="true"/>
  <property name="packagesToScan">
    <list>
      <value>io.github.gv0tch0.sotaro</value>
    </list>
  </property>
</bean>

如果Jaxb2Marshaller实现能够发现包含JAXB注释类的包中有一个ObjectFactory可包装响应为JAXBElement,则可以很好地工作,并以原始配置开箱即用。


我一整天都在苦苦挣扎,后来在 Stack Overflow 上找到了你的问题。它对我帮助很大,这也适用于 Github 上的示例。非常感谢你。 - Juan Carlos González

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接