您的位置:首页 > 编程语言 > Java开发

Struts2返回json格式数据踩坑记录

2016-12-27 20:01 483 查看

事件起因

昨天提测修改冻结/解冻银行卡样式的功能,微姐测试过程中发现调用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字段。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐