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



下面是创建pojo的json。我想用龙目创建一个pojo。我是新来的,可以放心。如何在Eclipse中使用Lombok创建pojo。我想要嵌套的json,如下面Jira API post-body请求。

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

我已经手动创建了下面的pojo,我不确定它是否正确。如何在post-body中调用生成的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"
}
}
}

最新更新