A toy example that captures the issue I am having is on github. In a nutshell I have a service that needs to marshal XML back over HTTP. I am trying to set that up using Spring Web MVC. The response object that needs to get marshalled back is JAXB2-generated from a schema (thus is missing @XmlRootElement tag but the package where it is generated has an ObjectFactory which provides the means to generate JAXBElement-s, which make XML marshallers happy). I tried different spring context configurations based on Google searches, which mostly hit posts here on stack overflow, but could not get any of them to work for me.
Environment:
- Spring 4.0.5.
- Tomcat 7.0.5X.
Here's a request/response cycle example that shows the problem (some of the output has been omitted):
$ 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>
Here are the toy example's modules.
One of the context configs I tried:
<?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:
@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;
}
}
Response Object (JAXB2 generated):
@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;
}
}
And the schema it is generated of:
<?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>