Struts2整合Json和MyBatis,懒加载对象无法被正确转化json的问题
项目采用Struts2,已经加入struts-json-plugin来整合Json,DAO层使用了MyBatis。
以下是相关信息:
struts.xml:
......
<package name="json" namespace="/" extends="json-default">
<action name="versions" class="com.hp.cmu.dbmanager.action.ProjectVersionAction">
<result name="json" type="json"/>
</action>
</package>
.......
Action:
public class ProjectVersionAction extends BaseAction {
private static final long serialVersionUID = -3683721108697436670L;
private ProjectVersionService projectVersionService;
private List<ProjectVersion> projectVersions;
private long projectId;
public String findVersionByProject() {
projectVersions = projectVersionService.findByProject(projectId);
return "json";
}
/*以提供所有的getter/setter*/
}
ProjectVersion类:
public class ProjectVersion implements Serializable {
private static final long serialVersionUID = 6222511923126470050L;
private Project project;
private long versionId;
private String version;
/*以提供getter/setter*/
}
Project类:
public class Project implements Serializable {
private static final long serialVersionUID = 947162450880795125L;
private long projectId;
private String projectName;
private String projectDesc;
private String activeFg;
/*以提供getter/setter*/
}
问题描述:
页面通过JQuery的ajax调用findVersionByProject方法,控制台报错如下:
10:32:58,780 DEBUG JSONWriter:72 - com.hp.cmu.dbmanager.model.ProjectVersion.getCallbacks()
java.lang.NoSuchMethodException: com.hp.cmu.dbmanager.model.ProjectVersion.getCallbacks()
at java.lang.Class.getMethod(Class.java:1605)
at org.apache.struts2.json.JSONWriter.findBaseAccessor(JSONWriter.java:266)
at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:194)
at org.apache.struts2.js