sidgate
sidgate

Reputation: 15244

JAXB annotations not working with Jackson message converter

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>

Upvotes: 1

Views: 2878

Answers (1)

s7vr
s7vr

Reputation: 75934

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.

Upvotes: 1

Related Questions