java Jackson将POJO转换为XML避免列表 Package 器

3xiyfsfu  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(130)

我试图得到一个没有根节点的列表输出。我将用一个更简单的想法来简化我的代码:

@JacksonXmlRootElement(localName = "my_company")
    public class Company {

     //Creating properties of Company class
     public String comName;
     public String comEmail;

     @JacksonXmlElementWrapper(useWrapping = false)
     public List<Parent> childs;

     // constructor
     Company(String comName, String comEmail, List<Parent> childs) {
        this.comName = comName;
        this.comEmail = comEmail;
        this.childs = childs;
     }

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
    class Parent{

    }
    @JsonTypeName("my_child")
     class Child extends Parent{
      public String street;
      public String state;
      public String city;

      Child(String street, String state, String city){
         this.street = street;
         this.state = state;
         this.city = city;
     }
  }

这是我得到的结果:

<my_company>
       <comName>A</comName>
       <comEmail>ema</comEmail>
       <childs>
         <my_child>
           <street>a</street>
           <state>b</state>
          <city>c</city>
        </my_child>
       </childs>
       <childs>
        <my_child>
          <street>a</street>
          <state>b</state>
          <city>c</city>
        </my_child>
       </childs>
   </my_company>

我不想这样,我需要删除“查尔兹”标签,我想这样:

<my_company>
        <comName>A</comName>
        <comEmail>ema</comEmail>
          <my_child>
             <street>a</street>
             <state>b</state>
             <city>c</city>
          </my_child>
         <my_child>
            <street>a</street>
            <state>b</state>
            <city>c</city>
         </my_child>
    </my_company>

***@JacksonXmlElementWrapper(useWrapping = false)***应该可以,但是我尝试了一百万种不同的组合、文档、论坛等。

1l5u6lss

1l5u6lss1#

在我指定解决方案之前,我想指出代码的几个问题:
1.它不能编译(Company类没有在构造函数中指定的addresscontacts示例变量)。

  1. Company类包含Parent项的列表,但Parent是一个空类,与Child没有关系。我删除了Parent
    解决方案是为CompanyList<Child>示例变量指定两个Jacksonxml注解:
    1.显而易见的@JacksonXmlElementWrapper(useWrapping = false)
  2. @JacksonXmlProperty(localName = "my_child")告诉Jackson列表项的名称
  3. Child类本身不需要注解。
    Company类如下所示
@JacksonXmlRootElement(localName = "my_company")
public class Company {

    //Creating properties of Company class
    public String comName;
    public String comEmail;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "my_child")
    public List<Child> childs;

    // constructor ...

试验方法

public static void main(String[] args) {

    List<Company.Child> childs = List.of(
            new Company.Child("a", "b", "c"),
            new Company.Child("d", "e", "f")
    );
    Company company = new Company("A", "ema", childs);
    try {
        XmlMapper mapper = new XmlMapper();
        mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, company);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出

<my_company>
  <comName>A</comName>
  <comEmail>ema</comEmail>
  <my_child>
    <street>a</street>
    <state>b</state>
    <city>c</city>
  </my_child>
  <my_child>
    <street>d</street>
    <state>e</state>
    <city>f</city>
  </my_child>
</my_company>

相关问题