【问题标题】:How to generate pojo with Lombok for complex json in Eclipse如何在 Eclipse 中使用 Lombok 为复杂的 json 生成 pojo
【发布时间】:2020-04-29 19:04:17
【问题描述】:

下面是 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】:

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"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    相关资源
    最近更新 更多