Struts2返回json格式数据踩坑记录
事件起因
昨天提测修改冻结/解冻银行卡样式的功能,微姐测试过程中发现调用ajax请求耗时过长,今天来排查,发现浏览器请求/finance/ajax/freeze/ajaxGetShopLists时,对应的后端方法getShopList()莫名其妙地执行了两边,并且返回给浏览器的Json字符串如下:
{
"accountId": null,
"accountInfoBean": {
"accountId": null,
"accountName": null,
"balance": null,
"bankAccountEffective": 0,
"bankAccountName": null,
"bankName": null,
"bankNumber": null,
"canWithDraw": null,
"canWithDrawNow": null,
"canWithdrawAmount": null,
"frozenReason": null,
"hadTransferTxToday": false,
"nextSettleDate": null,
"settleType": 0,
"status": 0,
"withDrawNowLimitBySubAccount": null,
"withdrawRuleString": null
},
"admin": false,
"code": 200,
"customerId": 401**976,
"customerList": null,
"customerName": "肖*",
"message": null,
"mobile": "1301***5995",
"msg": {
"customerName": "肖**",
"currentShopId": 0,
"accountId": null,
"shopList": [
{
"accountId": "210*******021",
"customerId": 0,
"displayName": "公司账户",
"shopId": 0,
"statusMessage": ""
}
],
"code": 200,
"mobile": "130****5995"
},
"productCode": 1,
"selectedShopName": "",
"shopAccountBeanList": [
{
"accountId": "2100*****021",
"customerId": 0,
"displayName": "公司账户",
"shopId": 0
}
],
"shopId": 0,
"shopList": "success",
"shopListJson": "[]"
}
而对应的方法如下:
public String getShopList() {
if (flag) {
loginAccountProfile = getLoginAccountProfile();
//初始化门店信息
code = SUCCESS_CODE;
initShopInfo();
getAccountIdStatus(shopAccountBeanList);
//获取客户名以及电话
getCustomerNameAndMobile();
msg.put("shopList", shopList);
msg.put("currentShopId", currentShopId);
msg.put("accountId", accountId);
msg.put("mobile", dealWithMobileNo(mobile));
msg.put("customerName", customerName);
msg.put("code", code);
flag = false;
}else{
logger.info("-------flag is false");
}
return SUCCESS;
}
对应的Struts配置如下
<package name="freezeAjax" namespace="/finance/ajax/freeze" extends="union-bill">
<action name="ajaxGetShopLists"
class="com.*****.AjaxGetShopList" method="getShopList">
<result type="json" name="success"></result>
</action>
</package>
从上面返回的Json字符串内容来看,返回字段并不是getShopList()方法中指定的字段,而是包含了com.*****.AjaxGetShopList类中所有get方法对应的字段。
初步推测getShopList()执行两次的过程如下:
- 后端struts框架接收到浏览器发送的请求,找到配置对应的action,将请求交给action对应的method来处理
- action对应的method处理完成后,根据xml中配置的内容返回结果给浏览器。网上搜索struts返回json的配置格式如下
<action name="jsonAction" class="com.ljq.action.JsonAction">
<result type="json">
<!-- 此处将reslut的值返回给客户端,root的值对应要返回的值的属性result
注意:root为固定写法,否则不会把result的值返回给客户端 -->
<param name="root">result</param>
</result>
</action>
所以推测是由于xml配置有问题,导致返回结果给浏览器时,将com.*****.AjaxGetShopList类中所有的get方法都执行了一遍。
尝试修改
将struts配置文件修改如下
<package name="freezeAjax" namespace="/finance/ajax/freeze" extends="union-bill">
<action name="ajaxGetShopLists"
class="com.*****.AjaxGetShopList" method="getShopList">
<!--
<result type="json"></result>
-->
<result type="json">
<param name="root">msg</param>
</result>
</action>
</package>
此时getShopList()方法只被调用一次,返回给浏览器的json格式如下:
{
"customerName": "饶总",
"currentShopId": 0,
"accountId": null,
"shopList": [
{
"accountId": "2100*****214",
"customerId": 0,
"displayName": "公司账户",
"freezeStatus": 0,
"shopId": 0,
"statusMessage": ""
}
],
"code": 200,
"mobile": "183****5421"
}
推测成立。在AjaxGetShopList类中增加一个getTestString()方法,使用之前错误的配置方式,无论AjaxGetShopList类中存不存在testString字段,返回给浏览器的json字符串中会多一个testString字段。
Struts2返回json格式数据踩坑记录的更多相关文章
- Struts2 返回 json 格式数据
最近由于工作原因,没时间更新,那些没能看到的同学,很抱歉了,话不多说,继续写~~ 关于json的返回需要用到一个工具包来将书转换为json格式,在此用到的jar包为: import net.sf.js ...
- 使用jQuery发送POST,Ajax请求返回JSON格式数据
问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- 返回json格式数据乱码
本文为博主原创,未经允许不得转载: 原本返回json格式数据的代码: @ResponseBody @RequestMapping(value = "getListByTime", ...
- WebService返回json格式数据供苹果或者安卓程序调用
1.新建一个WebService. 2. /// <summary> /// DemoToJson 的摘要说明 /// </summary> [WebService(Names ...
- Django 1.8.11 查询数据库返回JSON格式数据
Django 1.8.11 查询数据库返回JSON格式数据 和前端交互全部使用JSON,如何将数据库查询结果转换成JSON格式 环境 Win10 Python2.7 Django 1.8.11 返回多 ...
- ajax请求后台,返回json格式数据,模板!
添加一个用户的时候,需要找出公司下所有的部门,和相应部门下的角色,利用ajax请求,实现联动技术.将返回的json格式数据,添加到select标签下. <script type="te ...
- 如何让webapi只返回json格式数据
最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
随机推荐
- AspNetPager 多条件分页查询
AspNetPager 这个分页控件一般做后台基本都知道的,我就不多说了(说明与下载链接:http://www.webdiyer.com/Controls/AspNetPager),嘿嘿!其实我也是刚 ...
- Wiki安装(PHP +Sqlite+Cache)
前期准备 PHP http://windows.php.net/download WinCache Extension for PHP URL:http://sourceforge.net/pro ...
- [游戏开发-学习笔记]菜鸟慢慢飞(四)-Camera
游戏开发中,主相机应该是最重要的GameObject之一,毕竟游戏呈现给玩家,就是通过它. 相机的使用,在不同的游戏中,有很大的不同.这里总结一下自己学到的一些相关知识. 固定位置-游戏过程中相机的T ...
- weblogic的集群与配置
目录(?)[-] 1.Weblogic的集群 2.创建Weblogic集群前的规划 3.开始创建我们的Weblogic集群 1.1 创建集群的总控制端aminserver 2.2 创建集群中的节点my ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- WPF实现三星手机充电界面
GitHub地址:https://github.com/ptddqr/wpf-samsung-phone-s5-charging-ui/tree/master 先上效果图 这个效果来自于三星S5的充电 ...
- .NET Core中ADO.NET SqlClient的使用与常见问题
一.简介 在很多要求性能的项目中,我们都要使用传统的ADO.NET的方式来完成我们日常的工作:目前有一些网友问有关于.NET Core操作SQL Server的问题在本文中解答一下. 本文旨在指出,在 ...
- [网站性能1]对.net系统架构改造的一点经验和教训
文章来源:http://www.admin10000.com/document/2111.html 在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方案,这不仅仅是因为 ...
- 装X代码
装X代码 http://hackcode.ishoulu.com/scp/ http://hackcode.ishoulu.com/umbrella/ http://hackcode.ishoulu. ...
- dateRangePicker时间范围控件
Github:https://github.com/dangrossman/bootstrap-daterangepicker/ 使用daterangepicker()为元素创建一个时间范围控件 &l ...