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>