JAXB 注释不适用于 Jackson 消息转换器
JAXB annotations not working with Jackson message converter
我有一个 spring 网络项目,我在其中更新了几个 jar,类路径同时包含 JAXB 和 Jackson XML 数据格式 jar。我正在尝试使用 Jackson XML 消息转换器从我的控制器获得预期的 XML 输出,但 JAXB 注释不起作用。有人可以帮忙吗?
包-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; }
}
}
预期输出 当我明确设置 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>
不正确,使用默认消息转换器(使用 MappingJackson2XmlHttpMessageConverter
)
<UserDemographics xmlns="">
<demographic>
<demographic><name>ADDRESS</name><value>Mall Road</value></demographic>
<demographic><name>COUNTRY</name><value>India</value></demographic>
</demographic>
</UserDemographics>
当我尝试使用以下代码设置 AnnotationIntrospector
时
@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));
}
我收到以下错误回复
<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>
我有一个 spring 网络项目,我在其中更新了几个 jar,类路径同时包含 JAXB 和 Jackson XML 数据格式 jar。我正在尝试使用 Jackson XML 消息转换器从我的控制器获得预期的 XML 输出,但 JAXB 注释不起作用。有人可以帮忙吗?
包-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; }
}
}
预期输出 当我明确设置 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>
不正确,使用默认消息转换器(使用 MappingJackson2XmlHttpMessageConverter
)
<UserDemographics xmlns="">
<demographic>
<demographic><name>ADDRESS</name><value>Mall Road</value></demographic>
<demographic><name>COUNTRY</name><value>India</value></demographic>
</demographic>
</UserDemographics>
当我尝试使用以下代码设置 AnnotationIntrospector
时
@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));
}
我收到以下错误回复
<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>