Struts2 + Json项目的序列化 [英] Struts2 + Json Serialization of items

查看:99
本文介绍了Struts2 + Json项目的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几类:

  public class Student {
private Long id;
private String firstName;
private String lastName;
私人套餐<注册> enroll = new HashSet< Enrollment>();
// Setter and getters
}

公开课登记{
私立学生;
私人课程;
Long enrollId;

// Setters and Getters
}

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

  @ParentPackage(json-default)
公共类JsonAction扩展了ActionSupport {

私人学生;

@Autowired
DbService dbService;

public String populate(){
returnpopulate;
}
$ b @Action(value =/ getJson,results = {
@Result(name =success,type =json)})
public String test(){
student = dbService.getSudent(new Long(1));
返回成功;
}

@JSON(name =student)
public Student getStudent(){
return student;
}
public void setStudent(Student student){
this.student = student;
}

}

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

解决方案

请参阅此处显示的答案使用包含和排除属性。我不认为这个例子清楚地显示了排除嵌套对象,但是我已经将它用于此目的。如果你仍然有问题,我会发布一个正则表达式来证明这一点。



在Struts 2中使用Json插件的问题

编辑
这是一个在注释中使用排除属性的例子,它阻止了嵌套成员的序列化:

  @ParentPackage(json-default)
@Result(type =json,params = {
excludeProperties,
^ inventoryHistory \\ [\\d + \\] \\.intrnmst,selectedTransactionNames,transactionNames
})
public class InventoryHistoryAction extends ActionSupport {
...
InventoryHistory是JPA实体对象的InventoryHistory类型,intrnmst引用另一个表,但是如果它是序列化的,则会由于延迟加载而导致异常,如果该操作是JSON串行因为这个原因,已经添加了exclude参数来防止这种情况。



请注意,

  \\ 

对于每个\字符都是必需的,所以一个\只会用在xml中,其中两个是必需的,因为转义字符串被解析。


I have the following classes:

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
}

I have Struts2 controller and I would like to to return Serialized instance of Class Student only.

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

}

It returns me the serializable student object with all sub classes, but I would like to have only student object without the hashset returned . How can I tell Struts to serialize only the object? I do have Lazy loading enabled and hashset is returned as proxy class.

解决方案

See the answer here which shows the use of include and exclude properties. I don't think the example clearly shows excluding nested objects however I have used it for this purpose. If you still have issues I'll post a regex which will demonstrate this.

Problem with Json plugin in Struts 2

Edit: Here is an example of using exclude properties in an annotation which blocks the serialization of a nested member:

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

inventoryHistory is of type InventoryHistory a JPA entity object, intrnmst references another table but because of lazy loading if it were serialized it would cause an Exception when the action is JSON serialized for this reason the exclude parameter has been added to prevent this.

Note that

\\ 

is required for each \ character, so a single \ would only be used in the xml where two are required because of escaping for the string to be parsed right.

这篇关于Struts2 + Json项目的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆