There is a Snapshot class with two nested classes - Person and Department:

public class Snapshot { public Snapshot() {} public Snapshot(String diffId, String organization, Person person, Departments departmentrs) { this.diffId = diffId; this.organization = organization; this.person = person; this.departments = departments; } private String diffId; private String organization; @JsonProperty("person") private Person person; @JsonProperty("departments") private Departments departments; public String getDiffId() { return diffId; } public void setDiffId(String diffId) { this.diffId = diffId; } public String getOrganization() { return organization; } public void setOrganization(String organization) { this.organization = organization; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public Departments getDepartments() { return departments; } public void setDepartments(Departments departments) { this.departments = departments; } public class Person { public Person() {}; public Person(String id, String fullName, String mobilePhone, List list, List<?> allids, Boolean general, Boolean isWho, Boolean rvz) { this.id = id; this.fullName = fullName; this.mobilePhone = mobilePhone; this.appontments = list; this.allids = allids; this.general = general; this.isWho = isWho; this.rvz = rvz; } private String id; private String fullName; private String mobilePhone; private List appontments; // Set of appointments private List<?> allids; private String name; private Boolean general; private Boolean isWho; private Boolean rvz; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getMobilePhone() { return mobilePhone; } public void setMobilePhone(String mobilePhone) { this.mobilePhone = mobilePhone; } public List getAppontments() { return appontments; } public void setAppontments(List appontments) { this.appontments = appontments; } public List<?> getAllids() { return allids; } public void setAllids(List<?> allids) { this.allids = allids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getGeneral() { return general; } public void setGeneral(Boolean general) { this.general = general; } public Boolean getIsWho() { return isWho; } public void setIsWho(Boolean isWho) { this.isWho = isWho; } public Boolean getRvz() { return rvz; } public void setRvz(Boolean rvz) { this.rvz = rvz; } } @Override public String toString() { return "IDocsSnapshot [diffId=" + diffId + ", organization=" + organization + "]"; } public class Departments { public Departments() {}; public Departments(String id, String departmentName, String allids, String parentName, String shortName, String index) { this.id = id; this.departmentName = departmentName; this.allids = allids; this.parentName = parentName; this.shortName = shortName; this.index = index; } private String id; private String departmentName; private String allids; private String parentName; private String shortName; private String index; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } public String getAllids() { return allids; } public void setAllids(String allids) { this.allids = allids; } public String getParentName() { return parentName; } public void setParentName(String parentName) { this.parentName = parentName; } public String getShortName() { return shortName; } public void setShortName(String shortName) { this.shortName = shortName; } public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } } 

}

How to convert objects of this class in json, using jackson? What annotations need to connect?

  • I used GBS for this task, google it, it can help. - Asidert
  • one
    @ Alexey Shimansky needs to be written about how to serialize in Json using different libraries;) - Alex Chermenin
  • one
    @AlexChermenin yes, I had such a thought. maybe somehow ... - Alexey Shimansky

1 answer 1

If you need to send JSON on the face from the control, then just stuff it in return, in the method that is annotated like this

 @GetMapping(path = URL_SEARCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public List<UserDto> search... 

And if you just need a JSON object (I don’t understand why it’s wise to use it in Java), then like this:

 ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(object);