gpt4 book ai didi

java - 属性的 fastxml 序列化

转载 作者:行者123 更新时间:2023-12-02 09:31:02 25 4
gpt4 key购买 nike

我正在使用 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;

...
}

关于java - 属性的 fastxml 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57981047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com