1

I am trying to get response text from Java server using getJSON() jQuery method. Although, I can get response data when the Java class is simple format (String, List and Map), I could not get success data when using other Java object.

The following is Java class which is using a simple type and I get the success result with data that works:

package com.awitd.framework.action;

import com.opensymphony.xwork2.Action;

public class getAllJson implements Action{

private String data;

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public String execute() {
    System.out.println(" this is from action");
    data = "[";
            data += "{";
            data += "\"objid\":\"" + "1" + "\",";
            data += "\"id\":\"" + "1" + "\",\"name\":\"" + "name" + "\"";
            data += "}"; System.out.println("data " + data);
        
    data += "]";
    
    return SUCCESS;
}


}

The following is Java class which is using other Java object and it doesn't return a success data:

package com.awitd.framework.action;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.awitd.framework.entity.Employee;
import com.awitd.framework.entity.Profile;
import com.awitd.framework.service.EmployeeService;
public class getAllJson implements Action{

private String data;

private EmployeeService employeeService;



private List<Employee> employeeList;
private Employee employee;
private Profile profile;
public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public EmployeeService getEmployeeService() {
    return employeeService;
}

public void setEmployeeService(EmployeeService employeeService) {
    this.employeeService = employeeService;
}

public String execute() {
    System.out.println(" this is from action");
    data = "[";
            /*data += "{";
            data += "\"objid\":\"" + "1" + "\",";
            data += "\"id\":\"" + "1" + "\",\"name\":\"" + "name" + "\"";
            data += "}"; System.out.println("data " + data);*/
        

    
    employeeList = employeeService.getAll();    
    System.out.println("size........"+employeeList.size()); 
    if (!employeeList.isEmpty()) {
        for (int i=0; i<employeeList.size(); i++) {
            employee = employeeList.get(i);
            profile = employee.getProfile();
            data += "{";
            data += "\"objid\":\"" + employee.getEmployeeId() + "\",";
            data += "\"id\":\"" + employee.getId() + "\",\"name\":\"" + employee.getName() + "\"";
            data += ",\"dob\":\"" + profile.getDob() + "\",\"sex\":\"" + profile.getSex() + "\"";
            data += ",\"email\":\"" + profile.getEmail() + "\",\"workstart\":\"" + profile.getWorkstart() + "\"";
            data += ",\"study\":\"" + profile.getStudySub() + "\",\"jplevel\":\"" + profile.getJpLevel() + "\"";
            data += ",\"jpgroup\":\"" + profile.getJpGroup() + "\",\"remark\":\"" + profile.getRemark() + "\"";
            data += "}";
            if (!(i==employeeList.size()-1))
                data += ","; 
        }
    }
    data += "]";
    
    
    
    
    return SUCCESS;
}


 }

Got this error:

No existing transaction found for transaction marked with propagation 'mandatory'

java.lang.reflect.InvocationTargetException    
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246)
org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102)
org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116)
org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:196)
org.apache.struts2.json.JSONResult.execute(JSONResult.java:170)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:367)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:271)
Roman C
  • 49,761
  • 33
  • 66
  • 176
Aung Thet
  • 3,051
  • 3
  • 13
  • 18
  • InvocationTargetException is just an abstraction layer over the real error. Please find the real error down the stacktrace (caused by... ) and post it here – Andrea Ligios Dec 03 '14 at 10:08

1 Answers1

1

Try the following code, it should fix the error

data += "{";
data += "\"objid\":\"" + employee.getEmployeeId() + "\",";
data += "\"id\":\"" + employee.getId() + "\",\"name\":\"" + employee.getName() + "\",";
data += ",\"dob\":\"" + profile.getDob() + "\",\"sex\":\"" + profile.getSex() + "\",";
data += ",\"email\":\"" + profile.getEmail() + "\",\"workstart\":\"" + profile.getWorkstart() + "\",";
data += ",\"study\":\"" + profile.getStudySub() + "\",\"jplevel\":\"" + profile.getJpLevel() + "\",";
data += ",\"jpgroup\":\"" + profile.getJpGroup() + "\",\"remark\":\"" + profile.getRemark() + "\"";
data += "}";
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I can't even use "EmployeeService employeeService" I tried by using simple query that was OK!. So I think the JSON writer can't cast employeeservice to JSON type object. – Aung Thet Dec 04 '14 at 03:10
  • You shouldn't use json result to serialize `employeeService`. See how to exclude properties from process [here](http://stackoverflow.com/a/21350079/573032). – Roman C Dec 04 '14 at 09:12