一个能够展现我所遇到问题的玩具例子可以在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. DeinumJAXBElement
,则需要将Jaxb2Marshaller
的supportJaxbElementClass
属性配置为true
。一旦这样做了,并且响应对象当然被包装在JAXBElement
中,那么世界上就一切都好了。 - gv0tch0JAXBElement
包装,它需要被JAXB注释并具有@XmlRootElement注释。除非编组实现(例如EclipseLink MOXy)支持并提供外部绑定源。 - gv0tch0