如何使用Lombok在Eclipse中为复杂的json生成pojo

问题描述 投票:0回答:1

下面是用于创建pojo的json。我想使用Lombok创建一个pojo。我是新来的放心。如何在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;
  }

  }
java json eclipse rest-assured lombok
1个回答
0
投票

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);

    given().log().all().body(a4).when().post("").then().statusCode(200);
}

您应该能够在控制台中看到以下内容

{
    "fields": {
        "project": {
            "key": "RA"
        },
        "summary": "Main order flow broken",
        "description": "Creating my fist bug",
        "issuetype": {
            "name": "Bug"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.