0

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

The following is an 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 an 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)
1
  • InvocationTargetException is just an abstraction layer over the real error. Please find the real error down the stacktrace (caused by... ) and post it here Commented Dec 3, 2014 at 10:08

1 Answer 1

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 += "}";
Sign up to request clarification or add additional context in comments.

2 Comments

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.
You shouldn't use json result to serialize employeeService. See how to exclude properties from process here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.