2

According to Spring MVC documentation, <mvc:annotation-driven/> configures support for JSON if Jackson is in the classpath, and support for XML if JAXB is present in the classpath. Simply by adding a Jackson dependency to my pom.xml, I get JSON support to work! (see: Ajax Simplification in Spring 3.0)

However, after trying to access the same service with accept header "application/xml", I get a 406 Not Acceptable response. What's the simplest way to get JAXB in the classpath? What is necessary to enable support for XML MarshallingHttpMessageConverter?

Update

Taking a look at AnnotationDrivenBeanDefinitionParser, I can see what defines if "jaxb2Present". I set a breakpoint around line 179 to see if the Jaxb2RootElementHttpMessageConverter is indeed being registered like the MappingJacksonHttpMessageConverter is. It isn't...

What's the simplest way to add JAXB to the classpath to make it automatically serialize my XML requests?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Mr. Springy
  • 21
  • 1
  • 2

2 Answers2

2

It should work. Make sure that the object being returned has @XmlRootElement annotation as required by JAXB.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • I also have the same problem, and it doesn't work like so. I have another question about this, but with all my configurations and classes specified (https://stackoverflow.com/questions/55381597/in-spring-mvc-what-is-the-correct-configuration-for-using-jaxb-annotations-for) and all seems right.. Spring MVC just doesn't work so simply with XML. – amportugal Mar 28 '19 at 15:17
0

If you're using Java 6, JAXB is already on the classpath. If you're using Java 5, you'll need to add a reference implementation yourself.

If you're using Maven, you can add to your pom.xml:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2</version>
</dependency>
earldouglas
  • 13,265
  • 5
  • 41
  • 50
  • 1
    In addition to the reference implementation there are the MOXy (http://www.eclipse.org/eclipselink/moxy.php), and JaxMe (http://ws.apache.org/jaxme/) JAXB implementations. – bdoughan Sep 15 '10 at 20:23