Tuesday, December 14, 2021

How spring rest and jackson convert java pojo to json behind the scene


Lets see a practical example-

POJO:

public class Student{

private String firstName;

private String lastName;

public Student(String firstName,String lastName){

    this.firstName = firstName;

    this.lastName = lastName;

}

public void setFirstName(String firstName){

      this.firstName = firstName;

}

public String getFirstName(){

     return firstName;

}

public void setLastName(String lastName){

      this.lastName= lastName;

}

public String getLastName(){

     return lastName;

  }

}

RestController:

@RestController

@RequestMapping("/api")

public class StudentRestController{

@GetMapping("/students")

public List<Student> getStudents(){

        List<Student> students = new ArrayList<Student>();

        students.add(new Student("lokman","hossain"));

        students.add(new Student("sadia","muna"));

    return students;

    }

}

If we hit to that given end point (/api/students/) it will send a json array with two json object . So how it convert into json .

jackson convert the java pojo into json and sends back to the client .



















No comments:

Post a Comment

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...