简体   繁体   中英

How to generate pojo with Lombok for complex json in Eclipse

Below is the json for pojo creation. I want to create a pojo using Lombok. I am new to rest assured. How can I create a pojo using Lombok in Eclipse. I want in for nested json, like below Jira API post body request.

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

I have created the below pojo manually, and I am not sure if it's correct. How can I call the generated pojo in post body?

@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;
  }

  }

The POJO is correct, It had some typos which I have corrected

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;

}
}

and the below is how you can test

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

You should be able to see the below in the console

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM