属性的 FasterXML 序列化



我正在按照以下内容使用fasterxml2.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;
...
}

相关内容

  • 没有找到相关文章

最新更新