Wednesday 22 January 2014

Struts2- Gson Response Example

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.Download Google Gson Jar either add maven dependency:-

pom.xml

 com.google.code.gson
 gson
 2.2.4


Two Main method we use for conversion :-
1. toJson(Object src) 
   //This method serializes the specified object into its equivalent Json representation.
2. fromJson(JsonElement json, Class XXXX)
   //This method deserializes the Json read from the specified parse tree into an object of the specified type.

Gson Goals or benefit:-

1:- Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
2:- Allow pre-existing unmodifiable objects to be converted to and from JSON
3:- Extensive support of Java Generics
4:- Allow custom representations for objects
5:- Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)


Here is Sample Code how to work :-

web.xml


 Struts2Gson

 
  struts2
  org.apache.struts2.dispatcher.FilterDispatcher
 

 
  struts2
  /*
 



 
  jsonResponse.jsp
 



struts.xml





 
 
 
 
 
  
 


JsonAction.java
package com.javastoreroom.display.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.javastoreroom.display.bean.Employee;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class JsonAction extends ActionSupport {

 /**
  * @author Arun
  */
 private static final long serialVersionUID = 1L;
 
 
private List  list = null;
public String fetchName(){
HttpServletResponse  response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
  PrintWriter out = null;
  try{
   out = response.getWriter();
   response.setContentType("text/html;charset=UTF-8");
   List emplList = getListEmployees();
   Gson gson  = new Gson();
   String content = gson.toJson(emplList);
   System.out.println(content);
      out.println("{\"Employee\":"+content+"}");
  }catch (IOException e) {
   e.printStackTrace();
  }finally{
   out.flush();
   out.close();
  }
  return null;
 }
 
  private List getListEmployees() {
  list = new ArrayList();
  {
   list.add(new Employee(1, "Arun"));
   list.add(new Employee(2, "Anu"));
   list.add(new Employee(3, "Aryan"));
   list.add(new Employee(4, "Alex"));
   list.add(new Employee(5, "Jass"));
   list.add(new Employee(6, "Jassica"));
   list.add(new Employee(7, "Akon"));
   list.add(new Employee(8, "Himesh"));
   list.add(new Employee(9, "Sonu"));
  }
  return list;
 }


 public List getEmployees() {
  return employees;
 }

 public void setEmployees(List employees) {
  this.employees = employees;
 }
}

#content a {
 padding: 4px;
 margin-bottom: 2px;
 background-color: #dedede;
 font-family: arial;
}

jsonResponse.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>




Json





After executing project you with below output in json or view format:-


JSON Response :-


:)

No comments:

Post a Comment