1

I have a spring web project, where I have updated few jars, classpath has both JAXB and Jackson XML dataformat jars. I am trying to get expected XML output from my controller with Jackson XML message converter, but the JAXB annotations are not working. Can someone please help?

package-info.java

@XmlSchema(xmlns = { 
    @XmlNs(prefix = "ac", namespaceURI = "http://www.example.com/ABC") 
    })
package com.example;

UserDemographics.java

@XmlRootElement(name = "user-demographics", namespace = "http://www.example.com/ABC")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserDemographics {

    @XmlElement(name = "demographic", namespace = "http://www.example.com/ABC")
    private Set<Demographic> demographics = new TreeSet<>();

    @XmlAttribute(name="user-id")
    private int userId;

    static class Demographic{
        private String key;
        private String value;

        @XmlAttribute(name = "name")
        public String getKey() { return key; }

        @XmlValue
        public String getValue() { return value; }
    }
}

Expected output Works when I explicitly set Jaxb2RootElementHttpMessageConverter

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ac:user-demographics xmlns:ac="http://www.example.com/ABC" user-id="2">
    <ac:demographic name="ADDRESS">Mall Road</ac:demographic>
    <ac:demographic name="COUNTRY">India</ac:demographic>
</ac:user-demographics>

Incorrect, with default message converters (Uses MappingJackson2XmlHttpMessageConverter)

<UserDemographics xmlns="">
    <demographic>
        <demographic><name>ADDRESS</name><value>Mall Road</value></demographic>
        <demographic><name>COUNTRY</name><value>India</value></demographic>
    </demographic>
</UserDemographics>

When I try to set AnnotationIntrospector using following code

@Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper xmlMapper = Jackson2ObjectMapperBuilder.xml().build();
    xmlMapper.setAnnotationIntrospector(
            AnnotationIntrospector.pair(
                    new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()), 
                    new JacksonAnnotationIntrospector()));
    converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));
  }

I get following incorrect response

<user-demographics xmlns="" xmlns="http://www.example.com/ABC" user-id="2">
    <demographic xmlns:zdef2091338567="" zdef2091338567:name="ADDRESS">Mall Road</demographic>
    <demographic xmlns:zdef112980045="" zdef112980045:name="COUNTRY">India</demographic>
</user-demographics>
sidgate
  • 14,650
  • 11
  • 68
  • 119
  • `Expected output Works when I explicitly set Jaxb2RootElementHttpMessageConverter` It seems you already have the answer. What do you want us to do? – 11thdimension Jun 20 '17 at 14:08
  • As I see, you have two problems, one is missing package level prefix which is [not supported](https://github.com/FasterXML/jackson-dataformat-xml/issues/18) and other is incorrect prefix which can be [resolved](https://stackoverflow.com/questions/14818134/xmlwriter-extends-attribute-name-with-zdef) by using different underlying stax library implementation for xml mapper. I don't see the second issue with latest version 2.8.7 xml databind lib which defaults to woodstox stax library. – s7vr Jun 20 '17 at 18:48
  • Just in case you are not aware which I think is not the case. You can always remove the xml databind library from classpath to default to `Jaxb2RootElementHttpMessageConverter` if that is option for you. – s7vr Jun 20 '17 at 18:57
  • @Veeram regarding "package level prefix not supported", shouldn't `jackson-module-jaxb-annotations` module support all JAXB annotations? – sidgate Jun 22 '17 at 08:25
  • Take a look at [javadocs](https://github.com/FasterXML/jackson-module-jaxb-annotations/blob/master/src/main/java/com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector.java#L29). Some of the annotations are not yet supported. You can watch the previously mentioned ticket for future updates. – s7vr Jun 22 '17 at 11:45

1 Answers1

1

As I see, you have two problems, one is missing package level prefix which is not supported and other is incorrect prefix which can be resolved by using different underlying stax library implementation for xml mapper.

I don't see the second issue with latest version 2.8.7 xml databind lib which defaults to woodstox stax library.

You can always remove the xml databind library from classpath to default to Jaxb2RootElementHttpMessageConverter if that is option for you.

Some of the annotations are not yet supported.

s7vr
  • 73,656
  • 11
  • 106
  • 127