Struts2 + Json序列化项目

我有以下课程:

public class Student { private Long id ; private String firstName; private String lastName; private Set enroll = new HashSet(); //Setters and getters } public class Enrollment { private Student student; private Course course; Long enrollId; //Setters and Getters } 

我有Struts2控制器,我想只返回Class Student的Serialized实例。

 @ParentPackage("json-default") public class JsonAction extends ActionSupport{ private Student student; @Autowired DbService dbService; public String populate(){ return "populate"; } @Action(value="/getJson", results = { @Result(name="success", type="json")}) public String test(){ student = dbService.getSudent(new Long(1)); return "success"; } @JSON(name="student") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } } 

它返回带有所有子类的可序列化学生对象,但我想只有没有返回hashset的student对象。 如何告诉Struts仅序列化对象? 我确实启用了延迟加载,并将hashset作为代理类返回。

请参阅此处的答案,其中显示了include和exclude属性的使用。 我不认为该示例清楚地显示了排除嵌套对象,但我已将其用于此目的。 如果你还有问题,我会发布一个正则表达式来certificate这一点。

Struts 2中的Json插件问题

编辑 :以下是在注释中使用排除属性的示例,该注释阻止嵌套成员的序列化:

 @ParentPackage("json-default") @Result(type = "json", params = { "excludeProperties", "^inventoryHistory\\[\\d+\\]\\.intrnmst, selectedTransactionNames, transactionNames" }) public class InventoryHistoryAction extends ActionSupport { ... 

inventoryHistory属于InventoryHistory类型的JPA实体对象,intrnmst引用另一个表但是由于延迟加载,如果它被序列化,则在操作为JSON序列化时会导致exception,因此添加了exclude参数以防止这种情况。

注意

 \\ 

每个\字符都是必需的,因此只有在xml中才会使用单个\,因为要转义正确解析的字符串,因此只需要两个\。

  @Controller @Results({ @Result(name="json",type="json" , params={"root","outDataMap","excludeNullProperties","true" ,"excludeProperties","^ret\\[\\d+\\]\\.city\\.province,^ret\\[\\d+\\]\\.enterprise\\.userinfos","enableGZIP","true" }) }) public class UserinfoAction extends BaseAction { @Action(value="login") public String login(){ if(jsonQueryParam!=null && jsonQueryParam.length()>0) { user = JsonMapper.fromJson(jsonQueryParam, TUserinfo.class); } Assert.notNull(user); //RESULT="ret" addOutJsonData: put List into outDataMap with key RESULT for struts2 JSONResult addOutJsonData(RESULT, service.login(user)); return JSON; } public class TUserinfo implements java.io.Serializable { private static final long serialVersionUID = 1L; private String userid; private String username; private String userpwd; private TEnterpriseinfo enterprise; private String telphone; private TCity city; ...... } public class TEnterpriseinfo implements java.io.Serializable { private String enterpriseid; private String enterprisename; private Set userinfos = new HashSet(0); .......} 

在设置excludeProperties属性之前,结果如下:

  {"ret":[ { "city":{"cityename":"tianjin","cityid":"12","cityname":"天津" ,"province": {"provinceename":"tianjing","provinceid":"02","provincename":"天津"} } ,"createddate":"2014-01-07T11:13:58" ,"enterprise":{"createddate":"2014-01-07T08:38:00","enterpriseid":"402880a5436a227501436a2277140000","enterprisename":"测试企业2","enterprisestate":0 ,"userinfos":[null,{"city":{"cityename":"beijing","cityid":"11","cityname":"北京","province":{"provinceename":"beijing","provinceid":"01","provincename":"北京市"} },"comments":"ceshi","createddate":"2004-05-07T21:23:44","enterprise":null,"lastlogindate":"2014-01-08T08:50:34","logincount":11,"telphone":"2","userid":"402880a5436a215101436a2156e10000","username":"0.5833032879881197","userpwd":"12","userstate":1,"usertype":0}] } ,"lastlogindate":"2014-01-08T10:32:43","logincount":0,"telphone":"2","userid":"402880a5436ab13701436ab1b74a0000","username":"testUser","userpwd":"333","userstate":1,"usertype":0}] } 

设置excludeProperties属性后,没有省和userinfos节点,结果如下:

 {"ret": [{ "city":{"cityename":"tianjin","cityid":"12","cityname":"天津"} ,"createddate":"2014-01-07T11:13:58" ,"enterprise":{"createddate":"2014-01-07T08:38:00","enterpriseid":"402880a5436a227501436a2277140000","enterprisename":"测试企业2","enterprisestate":0} ,"lastlogindate":"2014-01-08T11:05:32","logincount":0,"telphone":"2","userid":"402880a5436ab13701436ab1b74a0000","username":"testUser","userpwd":"333","userstate":1,"usertype":0 }] }