Getting exception while wrapping Data object into json object in a servlet for data table
Getting exception while wrapping Data object into json object in a servlet for data table
arunsingh
Posts: 7Questions: 0Answers: 0
Hi Alan,
I am working with datatables first time. I am making an json object in a servlet by calling methods to get object data and then bind it with json data. But I am getting below exception while executing:
WARNING: executePhase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@fb6dbd) threw exception
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
I am using JSF in jsp files. Data is coming very well from data object but could not bind into json object.
My code snippets:
Script for datatable:
function getTable()
{
$(document).ready(function() {
$('#example').dataTable({
'bProcessing': true,
'bServerSide': true,
'bRetrieve' : true,
'sAjaxSource': './AddNewUserTestServlet',
'bJQueryUI': true,
'aoColumns': [
{ 'mData': 'firstName' },
{ 'mData': 'lastName' },
{ 'mData': 'email' },
{ 'mData': 'subscriptionType' }
]
});
} );
}
function clearTable()
{
$('#example').dataTable().fnClearTable();
}
Servlet:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans;
import com.google.gson.*;
import datatableobject.DataTableObject;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import dbmapping.UsersTbl;
import dbopr.DBInformation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
public class AddNewUserTestServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* GET and
* POST methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
AddNewUser newUser=new AddNewUser();
//newUser.getCompanyName();
//Code for getting userlist according to company
//List userList = null;
List userList = new ArrayList();
//Getting company name from AddNewUserTest
String companyName="GridAllDeveloper/pune/India";
//Search method of AddNewUser.java
Integer companyId=null;
//userList.clear();
DBInformation dBInformation = new DBInformation();
int userId = 0, userType = 0;
if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap() != null) {
userId = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userId");
userType = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userType");
}
if (companyName != null) {
companyId = dBInformation.getCompanyId(companyName);
if (companyId != null) {
userList = dBInformation.getAllUsersByUserIdNCompanyId(userId, companyId);
}
}
dBInformation = null;
DataTableObject dataTableObject = new DataTableObject();
dataTableObject.setUserList(userList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dataTableObject);
out.print(json);
//processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
//processRequest(request, response);
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Kindly help me.
Regards,
Arun Singh
I am working with datatables first time. I am making an json object in a servlet by calling methods to get object data and then bind it with json data. But I am getting below exception while executing:
WARNING: executePhase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@fb6dbd) threw exception
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
I am using JSF in jsp files. Data is coming very well from data object but could not bind into json object.
My code snippets:
Script for datatable:
function getTable()
{
$(document).ready(function() {
$('#example').dataTable({
'bProcessing': true,
'bServerSide': true,
'bRetrieve' : true,
'sAjaxSource': './AddNewUserTestServlet',
'bJQueryUI': true,
'aoColumns': [
{ 'mData': 'firstName' },
{ 'mData': 'lastName' },
{ 'mData': 'email' },
{ 'mData': 'subscriptionType' }
]
});
} );
}
function clearTable()
{
$('#example').dataTable().fnClearTable();
}
Servlet:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans;
import com.google.gson.*;
import datatableobject.DataTableObject;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import dbmapping.UsersTbl;
import dbopr.DBInformation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
public class AddNewUserTestServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* GET and
* POST methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
AddNewUser newUser=new AddNewUser();
//newUser.getCompanyName();
//Code for getting userlist according to company
//List userList = null;
List userList = new ArrayList();
//Getting company name from AddNewUserTest
String companyName="GridAllDeveloper/pune/India";
//Search method of AddNewUser.java
Integer companyId=null;
//userList.clear();
DBInformation dBInformation = new DBInformation();
int userId = 0, userType = 0;
if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap() != null) {
userId = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userId");
userType = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userType");
}
if (companyName != null) {
companyId = dBInformation.getCompanyId(companyName);
if (companyId != null) {
userList = dBInformation.getAllUsersByUserIdNCompanyId(userId, companyId);
}
}
dBInformation = null;
DataTableObject dataTableObject = new DataTableObject();
dataTableObject.setUserList(userList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dataTableObject);
out.print(json);
//processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
//processRequest(request, response);
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Kindly help me.
Regards,
Arun Singh
This discussion has been closed.