Spring is a framework of java that is used to build high-level, large-scale enterprise applications. And the most common thing we do in almost every application is transfer data between the front-end and back-end (to the server). Supposedly, when we perform Update, Create, or any other operation that impacts changes in the Database then what happens is, data is sent in the form of a request from the client. And this request is received by the back end.
Now to exchange data between the front end and the backend, we have to use a common format which is – JavaScript Object Notation (JSON). Now, each time the data is received by the back-end, since we’re using java, java doesn’t understand what’s JSON. It stores data in the form of plain-old-java-objects (POJOs). So, this JSON Data needs to be converted into the form which java understands. So, if we were to convert data received or send in every request, there would be a lot of extra work and more code to do for programmers. To solve this problem, we’re provided with a library called – Jackson.
Jackson performs the conversion of POJO to JSON and JSON to POJO
Jackson is one of the most used libraries of spring boot which translates JSON data to a Java POJO by itself and vice-versa. JSON stands for JavaScript-object-Notation and POJO is our plain-old-java-object class. See the diagram below.
So basically, Jackson is a data-binding API. There are 2 types of data binding :
1. Simple Data Binding : This term is used when we are converting primitive objects like : Boolean, Number, Strings, maps in java to JSON. And vice-versa.
2. Full Data Binding : When ANY TYPE of object in java can be converted to JSON and vice-versa, then we say that this is FULL Data Binding.
Today, we’ll look at the example of full data binding
Step-by-Step Implementation
So first go, to spring Inializr and create project with the following requirements & dependencies :
Note : The Spring Web Dependency contains the Jackson projects.
We are using lombok library to save time for writing getters and setter, its completely OPTIONAL ! (You can write getter and setters yourself)
Download the zip file, then Extract it wherever you want. Now, go to IntelliJ > File > Open > Browse where you saved the extracted zip file then open it. Create a new package of your preferred name for storing the entity. Create a new class – Student, under this package. This will be the entity of our project.
Student Class
Java
package com.neveropen.jacksondemo.jackson.Entity; import lombok.Data; @Data public class Student { private int id; private String firstName; private String lastName; private String email; public Student( int i, String firstName, String lastName, String email) { this .id = i; this .firstName = firstName; this .lastName = lastName; this .email = email; } } |
Code Explanations
In the above code, we’ve created an entity class which is – Student. It’s basically the POJO we recently talked about and is used to store data in JAVA.
4 fields are defined: id (int), firstName (String), lastName (String), email (String).
Note: @Data is an Annotation of the Lombok library which reduces the need to write getters and Setters for us.
Now create another package for storing the REST Controllers of your project. Your final directory structure should look something like this:
Then create a new REST Controller to define a Request Mapping:
Rest Controller
Java
package com.neveropen.jacksondemo.jackson.RESTControllers; import com.neveropen.jacksondemo.jackson.Entity.Student; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping ( "/api" ) public class JacksonDemoController { @GetMapping ( "/students" ) public List<Student> getAllStudents() { List<Student> studentList = new ArrayList<>(); studentList.add( new Student( 1 , "Adwitiya" , "Mourya" , "adwitiya@example.com" )); studentList.add( new Student( 2 , "Paramjeet" , "singh" , "param@example.com" )); studentList.add( new Student( 3 , "Anish" , "Jagadevan" , "Anish56@example.com" )); studentList.add( new Student( 4 , "Juan" , "Philips" , "juan123@example.com" )); return studentList; } } |
Code Explanation
In the above code, we’ve annotated our Controller class with @RESTController to signify that this is our rest controller, and under the class, we’ve defined a @GetMapping on a method that returns a list of all the students. Now as you can see, here we are returning a JAVA Object (POJO) in the method. But all the data that are received on the client is in JSON Format.
Jackson further uses : ObjectMapper class behind the scene to convert JSON to POJO and POJO to JSON
The important thing to highlight here is that when we return a POJO, the Jackson project behind the scene will call the getter methods that we’ve defined in the entity class to convert Java POJO to JSON. See the diagram below :
In the above image, for POJO to JSON conversions, Getters are being called. The same will happen with the setter methods. Now let’s test our API in Postman
Testing On browser: