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": 40133976,
    "customerList": null,
    "customerName": "肖菁",
    "message": null,
    "mobile": "130******95",
    "msg": {
        "customerName": "肖菁",
        "currentShopId": 0,
        "accountId": null,
        "shopList": [
            {
                "accountId": "210****1",
                "customerId": 0,
                "displayName": "公司账户",
                "shopId": 0,
                "statusMessage": ""
            }
        ],
        "code": 200,
        "mobile": "130****5995"
    },
    "productCode": 1,
    "selectedShopName": "",
    "shopAccountBeanList": [
        {
            "accountId": "210002177021",
            "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.dianping.bb.merchant.finance.web.ajax.frezeeAccount.AjaxGetShopList" method="getShopList">
            <result type="json" name="success"></result>
        </action>
    </package>

从上面返回的Json字符串内容来看,返回字段并不是getShopList()方法中指定的字段,而是包含了com.dianping.bb.merchant.finance.web.ajax.frezeeAccount.AjaxGetShopList类中所有get方法对应的字段。

初步推测getShopList()执行两次的过程如下:

  1. 后端struts框架接收到浏览器发送的请求,找到配置对应的action,将请求交给action对应的method来处理
  2. 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.dianping.bb.merchant.finance.web.ajax.frezeeAccount.AjaxGetShopList类中所有的get方法都执行了一遍。

尝试修改

将struts配置文件修改如下

<package name="freezeAjax" namespace="/finance/ajax/freeze" extends="union-bill">
    <action name="ajaxGetShopLists"
            class="com.dianping.bb.merchant.finance.web.ajax.frezeeAccount.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": "210****214",
            "customerId": 0,
            "displayName": "公司账户",
            "freezeStatus": 0,
            "shopId": 0,
            "statusMessage": ""
        }
    ],
    "code": 200,
    "mobile": "183****5421"
}

推测成立。在AjaxGetShopList类中增加一个getTestString()方法,使用之前错误的配置方式,无论AjaxGetShopList类中存不存在testString字段,返回给浏览器的json字符串中会多一个testString字段。

猜你喜欢

转载自blog.csdn.net/umgsai/article/details/54288442
今日推荐