属性的 fasterxml 序列化
fasterxml serialization of an attribute
我在下面使用 fasterxml
2.9.4
并尝试将父 class 中的 CurrencyAndAmount
class 序列化为:
<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;
...
}
我在下面使用 fasterxml
2.9.4
并尝试将父 class 中的 CurrencyAndAmount
class 序列化为:
<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;
...
}