3

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>
gv0tch0
  • 391
  • 1
  • 2
  • 14
  • Have you tried returning `JAXBElement` instead. – M. Deinum Jun 04 '14 at 18:03
  • I just did. Did not solve the issue. Also made branch with changes available in the [github repo](https://github.com/gv0tch0/sotaro/compare/ReturningJAXBElement). – gv0tch0 Jun 04 '14 at 19:53
  • As it almost always is the case there is no substitute for reading some code. So the `Jaxb2Marshaller` needs to have its `supportJaxbElementClass` property configured to `true` if it is to support marshalling `JAXBElement`-s. Once this is the case and the response object is, of course, wrapped in a `JAXBElement` all is good in the world. – gv0tch0 Jun 05 '14 at 02:12
  • Wow.. The comment editing rules here on the stack are .. weird .. Anyhow, continuation of the previous comment: The code also revealed that if the response is not `JAXBElement`-wrapped it needs to be JAXB-annotated and feature an @XmlRootElement annotation. Unless the marshalling implementation, e.g. EclipseLink MOXy, supports and is given an external bindings source. – gv0tch0 Jun 05 '14 at 02:21

1 Answers1

2

So the culprit was a missing JAXBElement-wrapper around the response object and a Jaxb2Marshaller configured to support JAXBElement-wrapped responses.

The @Controller becomes:

@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);
  }
}

And the Jaxb2Marshaller-part of the spring configuration becomes:

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

It would have actually been nice if the Jaxb2Marshaller implementation discovered that the package where the JAXB annotated classes are contains an ObjectFactory capable of wrapping responses in JAXBElement-s and just worked out of the box with the original configuration.

gv0tch0
  • 391
  • 1
  • 2
  • 14
  • I've been struggling with this all the day and I found your question in SO. It was of a great help. This applies to the example in Github too. Thank you very much. – Juan Carlos González Aug 22 '14 at 16:34