@JacksonXmlProperty(isAttribute = true) 未将元素视为属性

问题描述

我正在尝试从 POJO 反序列化 XML。这是我的 POJO 课程:

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@JacksonXmlRootElement(localName = "user")
public final class test {

    @JacksonXmlProperty(isAttribute = true)
    private String attr = "help";
    @JacksonXmlText
    private String value;

    public test(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper xmlMapper = XmlMapper.builder().addModule(new ParameterNamesModule()).build();
        System.out.println(xmlMapper.writeValueAsstring(new test("help")));
    }
}

Actual Output:
<user><attr>help</attr>help</user>

Expected Output:
<user attr="help">help</user>

Libraries Used:
<dependency>
     <groupId>com.fasterxml.jackson.dataformat</groupId>
     <artifactId>jackson-dataformat-xml</artifactId>
     <version>2.12.3</version>
</dependency>

尽管设置了“@JacksonXmlProperty(isAttribute = true)”,但我无法弄清楚为什么将属性设置为元素。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)