【问题标题】:Jackson: How to add custom metadata to a xml tagJackson:如何将自定义元数据添加到 xml 标签
【发布时间】:2021-07-02 12:37:33
【问题描述】:

我正在尝试使用 Jackson 将 JSON 转换为 XML。我有以下 POJO

@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {

    private String id;
    private String name;
    private String type = "student";

}

以下是转换后的输出:-

<Person>
    <id>1</id>
    <name>abc</name>
    <type>student</type>
</Person>

但我想要的输出是:

 <Person>
    <id>1</id>
    <name type="student">abc</name>
</Person>

有没有办法做到这一点?任何帮助深表感谢。问候

【问题讨论】:

    标签: java xml jackson-databind


    【解决方案1】:

    我认为你需要使用@XmlAttribute:

    @XmlAttribute
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String method;
    

    请看这里:https://www.gitmemory.com/issue/spring-projects/spring-hateoas/794/667375029

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2017-12-30
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2021-09-19
      相关资源
      最近更新 更多