Building Project Structure :
- Download the Google GSON from this link download gson.
- Create a dynamic web project and put the gson jar file in lib folder of WEB-INF.Refer the screen shot below.
JsonDemoServlet :
-
Create a Java Servlet to process request from client (browser) .The response type of the servlet be application/JSON.
package com.sandeep.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sandeep.example.servlet.object.Student;
import com.sandeep.example.servlet.object.StudentDataService;
public class JsonDemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public JsonDemoServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ListlistOfStudent = StudentDataService.getStudentData();
String studentJsonString = StudentDataService.cerateStaticJSON(listOfStudent);;
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.write(studentJsonString);
}
}
Student Class
package com.sandeep.example.servlet.object;
import java.util.Date;
public class Student {
private String nameOfStudent;
private Date dateOfRegistration;
private int marks;
public String getNameOfStudent() {
return nameOfStudent;
}
public void setNameOfStudent(String nameOfStudent) {
this.nameOfStudent = nameOfStudent;
}
public Date getDateOfRegistration() {
return dateOfRegistration;
}
public void setDateOfRegistration(Date dateOfRegistration) {
this.dateOfRegistration = dateOfRegistration;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
Using this class create some student object.
Student student1=new Student();
listofstudent.add(student1);
…
…
The Student Data Service Class :-
package com.sandeep.example.servlet.object;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class StudentDataService {
/**
* Rturn List Of Student as POJO
*
* @return
*/
public static ListgetStudentData() {
ListlistOfStudent = new ArrayList ();
Student sudent1 = new Student();
Student sudent2 = new Student();
sudent1.setNameOfStudent("Sandeep");
sudent1.setDateOfRegistration(new Date());
sudent1.setMarks(95);
sudent2.setNameOfStudent("Surabhi");
sudent2.setDateOfRegistration(new Date());
sudent2.setMarks(65);
listOfStudent.add(sudent1);
listOfStudent.add(sudent2);
return listOfStudent;
}
/**
* Convert Student Java Object to JSON String
*
* @param studentList
* @return
*/
public static String cerateStaticJSON(ListstudentList) {
Listlist = getStudentData();
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
}
This class provides data for our demo.
The Output :-
Accessing the Link
http://localhost:8080/JsonWebDemo/JsonDemoServlet
gives
[{"nameOfStudent":"Sandeep","dateOfRegistration":"Nov 20, 2012 10:08:00AM",
"marks":95},{"nameOfStudent":"Surabhi","dateOfRegistration":"Nov 20, 2012 10:08:00 AM","marks":65}]
Gson gson = new Gson();
The GSON class object converts from /to JSON.
Prettifying The JSON Output :-
The output in the firefox screenshot is coming in a single lineThis format is ugly to read and understand.To make it beautiful we can do some changes in the code cerateStaticJSON method.
public static String cerateStaticJSON(ListstudentList) {
Listlist = getStudentData();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(list);
return json;
}
The Changed Output:-
Now The ouput is properly aligned and formatted.
[
{
"nameOfStudent": "Sandeep",
"dateOfRegistration": "Nov 20, 2012 10:38:12 AM",
"marks": 95
},
{
"nameOfStudent": "Surabhi",
"dateOfRegistration": "Nov 20, 2012 10:38:12 AM",
"marks": 65
}
]
The GSonBuilder class is used for configuring the output format.The possible configuration are :-
Gson gson = new GsonBuilder()
.registerTypeAdapter(Id.class, new IdTypeAdapter())
.enableComplexMapKeySerialization()
.serializeNulls()
.setDateFormat(DateFormat.LONG)
- Sandeep
- Surabhi