I am new to json and dto. Tell me how to return json response to the browser page? (Using RestService)

In the answer to receive all interconnected trades and subjects. I have connected tables Many-to-many in this regard, I work with the dto layer

Profession

public class ProfessionDto { @JsonProperty("id") private int id; @JsonProperty("SubjectName") private String professionName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProfessionName() { return professionName; } public void setProfessionName(String professionName) { this.professionName = professionName; } public static ProfessionWithSubjectDto fromModel(Profession profession){ ProfessionWithSubjectDto dto =new ProfessionWithSubjectDto(); dto.setId(profession.getId()); dto.setProfessionName(profession.getProfessionName()); List<SubjectDto> subjectDtos = new ArrayList<>(); for(Subject subject : profession.getSubjects()){ subjectDtos.add(SubjectDto.fromModel(subject)); } dto.setSubjects(subjectDtos); return dto; 

}}

Subjects

  Public class SubjectDto { @JsonProperty("id") private Integer id;; @JsonProperty("SubjectName") private String subjectName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSubjectName() { return subjectName; } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } public static SubjectDto fromModel(Subject subject){ SubjectDto dto = new SubjectDto(); dto.setId(subject.getId()); dto.setSubjectName(subject.getSubjectName()); return dto; } 

}

Subject can have multiple professions

 public class ProfessionWithSubjectDto extends ProfessionDto { @JsonProperty("id") private int id; @JsonProperty("professionName") private String professionName; @JsonProperty("subjects") private List<SubjectDto> subjects; public void setId(int id) { this.id = id; } public int getId() { return id; } public String getProfessionName() { return professionName; } public void setProfessionName(String professionName) { this.professionName = professionName; } public List<SubjectDto> getSubjects() { return subjects; } public void setSubjects(List<SubjectDto> subjectDtos) { this.subjects = subjectDtos; } public static ProfessionWithSubjectDto fromModel(Profession profession){ ProfessionWithSubjectDto dto = new ProfessionWithSubjectDto(); dto.setId(profession.getId()); dto.setProfessionName(profession.getProfessionName()); List<SubjectDto> subjectDtos = new ArrayList<>(); for (Subject subject : profession.getSubjects()){ subjectDtos.add(SubjectDto.fromModel(subject)); } dto.setSubjects(subjectDtos); return dto; } 

}

A profession may have several subjects.

 public class SubjectWithProfessionDto { @JsonProperty("id") private int id; @JsonProperty("SubjectName") private String SubjectName; @JsonProperty("professions") private List<ProfessionDto> professions; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSubjectName() { return SubjectName; } public void setSubjectName(String subjectName) { SubjectName = subjectName; } public List<ProfessionDto> getProfessions() { return professions; } public void setProfessions(List<ProfessionDto> professions) { this.professions = professions; } public static SubjectWithProfessionDto fromModel (Subject subject){ SubjectWithProfessionDto dto = new SubjectWithProfessionDto(); dto.setId(subject.getId()); dto.setSubjectName(subject.getSubjectName()); List<ProfessionDto> professionDtos = new ArrayList<>(); for(Profession profession : subject.getProfessions()){ professionDtos.add(ProfessionDto.fromModel(profession)); } dto.setProfessions(professionDtos); return dto; } 

}

There is a layer Controller

In it, I want to return the data (JSON) to the page using @ResponseBody

  • And what is the difficulty? Already resulted in the @ResponseBody annotation. It does not work? - enzo
  • @enzo I do not know how to return specifically dto. How to return dao I understand, but dto not. - Nadezhda
  • @enzo @RequestMapping(value = "/me",method = GET) @ResponseBody public List<SubjectDto> getAll(){ List<Subject> subjects = subjectService.getAll(); List<SubjectDto> subjectDtos = new ArrayList<>(subjects.size()); subjects.forEach(subject -> subjectDtos.add(convertToDTO(subject))); @RequestMapping(value = "/me",method = GET) @ResponseBody public List<SubjectDto> getAll(){ List<Subject> subjects = subjectService.getAll(); List<SubjectDto> subjectDtos = new ArrayList<>(subjects.size()); subjects.forEach(subject -> subjectDtos.add(convertToDTO(subject))); return subjectDtos; `but the program swears at convertToDTO - Nadezhda
  • one
    Then you have a problem when converting an object to DTO. And here Spring and JSON? Your controller method should return an object. Spring itself converts this object to JSON - it doesn’t care DTO it or any other POJO object. You need to write a unit test for the convertToDTO() method and debug it. - enzo

1 answer 1

The easiest and coarsest way is to take the data structure and pass it through GSON. Give the resulting string return by adding the @ResponseBody annotation.

Read more here: https://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html

and here: https://github.com/google/gson

Why not standard spring json - gson has wider possibilities of working with complex objects.

  • And how to do it with JSON ?? It is necessary to do exactly through it - Nadezhda
  • Detailed instructions for creating a service that provides json data on request: devcolibri.com/2890 - DimXenon
  • If anything, when returning an object with the @ResponseBody annotation, spring will automatically try to return the json representation of the object, if everything is configured as it should (mappings in web.xml -> <url-pattern> / </ url-pattern>, support for @ResponseBody in mvc-dispatcher-servlet.xml). - DimXenon
  • I work through annotations. (@Controller), etc. It seems everything is configured correctly. Without using xml - Nadezhda
  • Good. Any errors occur when trying to return dto? Here is another guide, specifically on the spring and DTO: baeldung.com/… . Try to go through it in the test application. - DimXenon