0

I have an action that should return a JSON from an AJAX call, but it throws an exception instead.

Do you have any clue on what could be the cause of the problem ?

Here is my code:

@Results({
    @Result(name="json", type="json", params = {"root", "json"})
})

public class TestController{

    public String tableData() {
        String test = "{'winston':'testing'}";
        JSONObject json = JSONObject.fromObject(test);
        return "json";
    }
}

The exception thrown:

ERROR RestActionInvocation Exception processing the result.
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Winston
  • 49
  • 8

1 Answers1

1

There are two problems:

  1. The Struts2 json result will serialize a Java object into a JSON object on its own, hence
    you do NOT have to serialize it yourself;

  2. The property must be accessible from outside, hence you can NOT define it inside the action method, but you need to do it at action level. Only non-transient, class-level properties with a getter will be serialized (even with the root attribute).

The code you want is:

@Results({
    @Result(name = ActionSupport.SUCCESS, 
            type = "json", 
          params = {"root", "myJsonObject"})
})
public class TestController extends ActionSupport {

    private String myJsonObject;
    public String getMyJsonObject(){ 
        return myJsonObject; 
    }

    public String execute() {
        myJsonObject = "someValue";
        return SUCCESS;
    }
}

After making it work with a String, myJsonObject can then be turned into a Map, or into a Bean (for example a bean with a winston property to which assign a testing value, in order to return the

{"winston":"testing"}

JSON as in the example.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Hi Andrea, thanks for your answer, but it seems not working in struts2.5.8, I am using the code same to you and working in struts2.3, but after I upgrade to 2.5.8, it have this exception. Do you know what happen? Thanks again~ – Winston Mar 08 '17 at 03:03
  • Do I need to includes some new lib? – Winston Mar 08 '17 at 03:12
  • 2.5.8 is not the latest, try 2.5.10. BTW, if root is an object, does it implement Serializable ? – Andrea Ligios Mar 08 '17 at 08:51
  • yes, i have upgrade to 2.5.10, the root is an object, it still can't serializable. But I found i can use getModel with an correct object and it will override the json and return the correct json for me. – Winston Mar 08 '17 at 10:02
  • I don't see any model driven stuff in the question; if you post a different code, helping you will be a lot more difficult ;) – Andrea Ligios Mar 08 '17 at 10:10
  • Andrea, I have solved the problem using model driven after i post this code, and i can get the correct json now, thanks for you help, i am very appreciate.=) – Winston Mar 08 '17 at 10:16