簡體   English   中英

如何使用 Lombok 為 Eclipse 中的復雜 json 生成 pojo

[英]How to generate pojo with Lombok for complex json in Eclipse

下面是用於創建 pojo 的 json。 我想使用 Lombok 創建一個 pojo。 我是 rest 的新手,放心。 如何在 Eclipse 中使用 Lombok 創建一個 pojo。 我想要嵌套的 json,如下面的 Jira API 后正文請求。

{
    "fields": {
        "project": {
      "key": "RA"
    },
    "summary": "Main order flow broken",
    "description": "Creating my fist bug",
     "issuetype": {
      "name": "Bug"
    }
        }
} 

我手動創建了以下 pojo,我不確定它是否正確。 如何在帖子正文中調用生成的 pojo?

@Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  public  class createissue {
    private fieldss fields;

    @Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  public static class fieldss {
    private  Project poject;
    private  Sting summary;
    private  String description;
    private  Issuetype issuetypessuetype;
}

 @Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  public static class Project {
    private Sting key;
}
    @Data
  @JsonIgnoreProperties(ignoreUnknown = true)
  public static class Issuetype {
  private Sting name;
  }

  }

POJO 是正確的,它有一些錯字,我已更正

public class Lombok {

public static @Data class fieldss {

    private  Project project;
    private  String summary;
    private  String description;
    private  Issuetype issuetype;

}

public static @Data class createissue {

    private fieldss fields;

}

public static @Data class Issuetype {

    private String name;

}

public static @Data class Project {
    private String key;

}
}

以下是您如何測試

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Issuetype a1 = new Issuetype();
    a1.setName("Bug");

    Project a2 = new Project();
    a2.setKey("RA");

    fieldss a3 = new fieldss();
    a3.setDescription("Creating my fist bug");
    a3.setSummary("Main order flow broken");
    a3.setIssuetype(a1);
    a3.setProject(a2);

    createissue a4 = new createissue();
    a4.setFields(a3);

    ObjectMapper mapper = new ObjectMapper();

    String abc = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a4);

    System.out.println(abc);
}

您應該能夠在控制台中看到以下內容

{
    "fields": {
        "project": {
            "key": "RA"
        },
        "summary": "Main order flow broken",
        "description": "Creating my fist bug",
        "issuetype": {
            "name": "Bug"
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM