问题
I need to configure JSON Plugin (https://struts.apache.org/docs/json-plugin.html) with my current JAVA web application. What I've done:
1) Added to pom.xml
file all dependencies for the plugin.
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.24.1</version>
</dependency>
2) Extended the package in struts.xml with json-default
<package name="my-test-package" extends="xxx, json-default"
namespace="/my-test-package">
3) Added to my action the interceptor and result type of json
<action name="myMainAction"
class="org.com.action.myMainAction">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
<result type="json">
</result>
<param name="requiredAuthorities">F_ADD</param>
</action>
4) Created a Model class Marriage
with only setters/getters in it.
5) Created the Action class, named as myMainAction
with following code
private List <Marriage> data;
public List<Marriage> getData() {
return data;
}
public void setData(List<Marriage> data) {
System.out.println("Inside the setter ");
this.data = data;
}
@Override
public String execute() throws Exception {
System.out.println(data.get(0).getLastNameHe());
return SUCCESS;
}
6) Created a JSON
object and passed it via ajax
to the server. I can see through Google Chrome Console, that it sends the data and it does parse the JSON object and all data is in there. However, it does throw an exception in my Eclipse and also in Chrome Console:
Exception occurred during processing request: Incompatible types for property setData (CommonsLogger.java [qtp1144623021-22]) org.apache.struts2.json.JSONException: Incompatible types for property setData
Could you please point me to some right direction, it's almost the second day which i'm working on it but it doesn't seem to start working.
EDIT1: The data which is being sent to java side and I can see it via Chrome console.
{"data":{"recordId":"123","registrationDate":"20-07-2016","hisId":"","herId":"","lastNameHe":"Asd","firstNameHe":"Asd","middleNameHe":"Asd","workPlaceHe":"","educationHe"}}
This is the object after it has been JSON.stringify
-ed.
As you can see it has the root data
variable and then all the rest of the data goes under it. According to my logic ti has to find the setter
of the data in my java class and then call the setters/getters of that class.
P.S I renamed the name of the classes and etc so no worries about naming conventions) I do have the right things on my production code.
EDIT2:
Thanks for pointing out to my mistake @AleksandrM. Now It throws a very strange error:
Unparseable date: "20-07-2016" (CommonsLogger.java [qtp212900572-17]) java.text.ParseException: Unparseable date: "20-07-2016"
The json
variable for this part is registrationDate
and its implementation in JAVA:
private Date registrationDate;
public Date getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
As I understand it tries to set a string
to date
and it can't do it. Why the json-plugin can't convert it? Should I write something in the options/settings?
回答1:
You are expecting List<Marriage> data
in the Java code but your JSON object doesn't hold any array. Either change Java code to Marriage data
or post array of objects in JSON.
You may also want to define other interceptors, besides json
, for the action.
Create custom interceptor stack and reference it in your action configuration.
<interceptor-stack name="json-stack">
<interceptor-ref name="json" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
For the date string to Date
object conversion use default format (which is yyyy-MM-dd'T'HH:mm:ss
) or create a custom Struts type converter.
or use @JSON(format = "dd.MM.yyyy")
on the setter.
See this follow up question.
来源:https://stackoverflow.com/questions/38473281/incompatible-types-for-property-setdata-jsonstruts2-plugin