详解Struts2中json 相互引用死循环解决办法
作者:不怕冷的小熊 时间:2022-09-08 04:04:46
方法1 :利用Struts 2的支持的可配置结果,可以达到过滤器的效果。Action的处理结果配置支持正则表达式。
但是如果返回的对象是一个数组格式的Json数据。比如peson Bean中有对象persion1…person9,而我只要person1的json数据, 则可以用如下的正则表达式。
<struts>
<constant name="struts.objectFactory" value="spring"/>
<include file="struts-admin.xml"></include>
<package name="default" extends="json-default">
<action name="person" class="com.person.PersonAction" method="view">
<result type="json">
<param name="includeProperties">
person/[/d+/]/.person1
</param>>
</result>
</action>
</package>
</struts>
excludeProperties * 的用法与此类同,如果拦截的仅仅是一个对象,如果拦截掉person Bean的整个对象。
<struts>
<constant name="struts.objectFactory" value="spring"/>
<include file="struts-admin.xml"></include>
<package name="default" extends="json-default">
<action name="person" class="com.person.PersonAction" method="view">
<result type="json">
<param name="excludeProperties">
person
</param>>
</result>
</action>
</package>
</struts>
方法2:需要注意的是,如果用JSON插件把返回结果定为JSON。而JSON的原理是在ACTION中的get方法都会序列化
所以前面是get的方法只要没指定不序列化,都会执行。 如果该方法一定要命名为get*(比如实现了什么接口), 那么可以在该方法的前面加注解声明该方法不做序列化。
注解的方式为:@JSON(serialize=false)
除此之外,JSON注释还支持如下几个域:
serialize:设置是否序列化该属性
deserialize:设置是否反序列化该属性。
format:设置用于格式化输出、解析日期表单域的格式。例如”yyyy-MM-dd'T'HH:mm:ss”。
//使用注释语法来改变该属性序列化后的属性名
@JSON(name="newName")
public String getName()
{
return this.name;
}
需要引入 import org.apache.struts2.json.annotations.JSON;
@JSON(serialize=false)
public User getUser() {
return this.User;
}
@JSON(format="yyyy-MM-dd")
public Date getStartDate() {
return this.startDate;
}
来源:http://blog.csdn.net/fangaohua200/article/details/54566504
标签:struts2,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java中的实例初始化块(IIB)
2023-06-08 08:35:46
spring boot devtools在Idea中实现热部署方法
2023-07-20 02:31:25
java实现socket客户端连接服务端
2021-12-02 03:52:07
![](https://img.aspxhome.com/file/2023/9/69899_0s.png)
C#常用数据结构和算法总结
2021-10-31 22:05:29
![](https://img.aspxhome.com/file/2023/6/69206_0s.png)
Tomcat 实现WebSocket详细介绍
2023-08-12 21:22:11
![](https://img.aspxhome.com/file/2023/0/58150_0s.png)
对Java ArrayList的自动扩容机制示例讲解
2022-04-09 07:58:25
Netty与NIO超详细讲解
2022-08-16 00:44:37
![](https://img.aspxhome.com/file/2023/2/65362_0s.png)
IDEA实现添加 前进后退 到工具栏的操作
2021-08-30 21:34:48
![](https://img.aspxhome.com/file/2023/7/62687_0s.jpg)
JAVA实现单例模式的四种方法和一些特点
2023-11-02 05:38:20
Java泛型机制与反射原理相关知识总结
2023-11-11 06:02:15
![](https://img.aspxhome.com/file/2023/4/58914_0s.jpg)
Java之策略模式比较器案例讲解
2021-12-25 22:24:32
java 接口回调实例详解
2023-11-19 15:44:24
java实现微信红包 拼手气红包
2023-09-28 10:31:45
![](https://img.aspxhome.com/file/2023/3/67113_0s.jpg)
java web服务器实现跨域访问
2023-09-17 06:55:08
深入解析Java的Spring框架中bean的依赖注入
2023-12-20 18:50:52
![](https://img.aspxhome.com/file/2023/8/66188_0s.png)
基于Java的guava开源库工具类
2022-04-07 05:37:53
C#(int)中Convert、Parse、TryParse的区别
2023-06-25 03:19:50
如何解决修改StaticText的字体
2023-05-25 08:40:14
![](https://img.aspxhome.com/file/2023/9/66699_0s.png)
SpringBoot整合SpringTask实现定时任务的流程
2022-03-28 22:24:40
SpringBoot返回多种格式的数据的实现示例
2023-11-24 14:22:52
![](https://img.aspxhome.com/file/2023/0/59700_0s.png)