我正在按照以下内容使用fasterxml
2.9.4
,并尝试将父类中的CurrencyAndAmount
类序列化为:
<ParentClass Currency="USD">100000</ParentClass>
但相反,我正在下面。文档表明,我需要isAttribute
对我不起作用。我错过了什么?提前感谢您提供正确方向的任何指示。
<ParentClass>100000</ParentClass>
依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.9</version>
</dependency>
XmlMapper
配置:
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary));
POJO
:
public class CurrencyAndAmount {
@JacksonXmlText
protected BigDecimal value;
@JacksonXmlProperty(localName = "Currency", isAttribute = true)
protected String currency;
@JsonValue
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String value) {
this.currency = value;
}
}
您应该删除JsonValue
注释并简化POJO
以:
class CurrencyAndAmount {
@JacksonXmlText
protected BigDecimal value;
@JacksonXmlProperty(localName = "Currency", isAttribute = true)
protected String currency;
...
}