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 控制器,我想仅返回 Student 类的序列化实例。
@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 仅序列化对象? 我确实启用了延迟加载,并且哈希集作为代理类返回。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此处的答案,其中显示了包含和排除属性的用法。我不认为该示例清楚地显示了排除嵌套对象,但我已将其用于此目的。如果您仍然遇到问题,我将发布一个正则表达式来证明这一点。
Struts 2 中的 Json 插件出现问题
编辑:
以下是在注释中使用排除属性的示例,该注释阻止嵌套成员的序列化:
inventoryHistory 的类型为 InventoryHistory ,是一个 JPA 实体对象, intrnmst 引用另一个表,但由于延迟加载,如果它被序列化,则会在以下情况下导致异常:操作是 JSON 序列化的,因此添加了排除参数来防止这种情况发生。
请注意,
每个 \ 字符都是必需的,因此仅在需要两个 \ 的 xml 中使用单个 \,因为要对字符串进行转义才能正确解析。
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:
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.
设置extractProperties属性前,结果如下:
设置extractProperties属性后,不存在province和userinfos节点,结果如下:
before set the excludeProperties property,the result is below:
after set the excludeProperties property,there are not exist province and userinfos nodes, the result is below: