Struts2 + Json 项目的序列化

Struts2 + Json Serialization of items(Struts2 + Json 项目的序列化)

本文介绍了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 控制器,我想只返回 Class Student 的序列化实例.

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

}

它返回给我所有子类的可序列化学生对象,但我希望只有学生对象而不返回哈希集.如何告诉 Struts 只序列化对象?我确实启用了延迟加载,并且 hashset 作为代理类返回.

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.

Struts 2 中的 Json 插件问题

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

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 是 JPA 实体对象的 InventoryHistory 类型,intrnmst 引用另一个表,但由于延迟加载,如果它被序列化,当操作被 JSON 序列化时会导致异常,因此添加了 exclude 参数来防止这种情况.

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.

注意

\ 

对于每个 字符都是必需的,因此单个 只会在需要两个的 xml 中使用,因为要转义以便正确解析字符串.

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 项目的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Struts2 + Json 项目的序列化