2

I upgraded Struts from 2.3.7 to 2.3.33 (using Maven). While with version 2.3.7 my web application was working without any issues, after the upgrade to 2.3.33, I receive the following exception when trying to access a web page that contains an Ajax call to the server.

java.lang.NoSuchMethodError: org.apache.struts2.json.JSONWriter.setDateFormatter(Ljava/lang/String;)V at org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:106)[struts2-json-plugin-2.3.33.jar:] at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:203)[struts2-json-plugin-2.3.33.jar:] at org.apache.struts2.json.JSONResult.execute(JSONResult.java:177)[struts2-json-plugin-2.3.33.jar:] at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:372)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:276)[xwork-core-2.3.33.jar:2.3.33] at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)[struts2-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:168)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)[xwork-core-2.3.33.jar:2.3.33] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)[xwork-core-2.3.33.jar:2.3.33]

The error occurs when my Java Action sends the json response back to the client.

Below is the Ajax call I use:

  $.ajax({
        url :  "list-sample-json",
           //   data : options.data,
           //   dataType : "json",
    
        type: "POST",
        data: JSON.stringify({ sortParameters : sortParametersList, pageSize:options.data.pageSize, skip:options.data.skip }),
        contentType : "application/json; charset=utf-8",
        dataType : "json",
        success : function (result) {
        options.success(result);
        },
        error : function (response, status, error) {
        openErrorDialog(response, status, error);
                                    }
    });

My action is accessed without any issues and returns SUCCESS. As mentioned above, the problem seems to occur when Struts generates the JSON response for the client. Below is also my Java class with json as a result type.

@Result(name = "success", type = "json", params = { "ignoreHierarchy", "false",
        "includeProperties", "textsampleList.*, count" })
@Injectable
public class ListSampleJsonAction {

    @Override
    public String execute() {
        
        List<SampleDTO> sampleList = new ArrayList<>();
        sampleList.add(new SampleDTO("sample")) ;
        return SUCCESS;
    }
          
}

I would like to mention that until 2.3.16.3 that the above implementation was working and I was getting a response (with the requested data) from the server.

I use maven 3.0.4 and Java 1.7_079. Can anybody suggest what might have gone wrong?

Roman C
  • 49,761
  • 33
  • 66
  • 176
assuna
  • 135
  • 1
  • 1
  • 8

1 Answers1

1

The error java.lang.NoSuchMethodError: org.apache.struts2.json.JSONWriter.setDateFormatter(Ljava/lang/String;)V

The method is available at javadoc.

and from Github.

So you have as @HarKrishan mentioned in the comment a jar conflict version.

But if it's not then probably you need to have the correct version of asm libraries on your classpath.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • There is jar conflict – Har Krishan Jan 04 '18 at 13:17
  • No conflict what? – Roman C Jan 04 '18 at 13:21
  • 2
    The main cause of NoSuchMethodError is you might have compiled a class against a different version than what you are using at runtime. So there is jar conflict. – Har Krishan Jan 04 '18 at 13:30
  • 1
    @HarKrishan I didn't compile anything (the plugin) is downloaded from the maven repository. if you compile from github then you won't get `NoSuchMethodError` because it explicitly shows that it has a method reference. – Roman C Jan 04 '18 at 17:55
  • @RomanC I checked the asm library version and i use 3.3 which is the one included in xwork-core 2.3.33. I don't know if this is the right version. As for jar conflicts all my struts libraries are of the same version. I still have the same problem. – assuna Jan 08 '18 at 13:02
  • no i can't update Java. But i found the problem i had override JSONWriter in my project because something didn't work with the previous version. The overwritten JSONWriter didn't have the method setDateFormatter so I had to replace it. Thanks a lot for your comments, they really helped to find the problem. – assuna Jan 09 '18 at 11:09