項目

2011-01-25 48 views
4

我有以下類的Struts2 + JSON序列:項目

public class Student { 
    private Long id ; 
    private String firstName; 
    private String lastName; 
private Set<Enrollment> enroll = new HashSet<Enrollment>(); 
//Setters and getters 
} 

public class Enrollment { 
    private Student student; 
    private Course course; 
    Long enrollId; 

//Setters and Getters 
} 

我有Struts2的控制器和我想只返回類學生的序列化實例。

@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; 
} 

} 

它返回可序列化的學生對象與所有子類,但我想只有學生對象沒有返回的哈希集。 如何告訴Struts只序列化對象? 我確實已啓用延遲加載,並將hashset作爲代理類返回。

回答

7

看到這裏的答案,顯示了使用包括和排除屬性。我不認爲這個例子清楚地顯示了排除嵌套對象,但是我已經將它用於此目的。如果你仍然有問題,我會發佈一個正則表達式來證明這一點。

Problem with Json plugin in Struts 2

編輯: 下面是使用排除註釋中的哪些塊嵌套成員的序列化特性的一個示例:

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

inventoryHistory是類型的InventoryHistory JPA實體對象,intrnmst引用另一個表,但因爲延遲加載(如果它被序列化),當操作是JSON序列化時,會導致異常,因此添加了exclude參數以防止此情況。

注意

\\ 

需要對每個\字符,所以單\只會在需要因爲逃逸的字符串的兩個XML可以用來解析權。

+0

是的:)這是我的文章,但它不會解決任何問題。 – 2011-01-26 11:18:25

2
@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<TUserinfo> 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<TUserinfo> userinfos = new HashSet<TUserinfo>(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 
    }] 
}