1. 程式人生 > >Struts2返回json格式資料踩坑記錄

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欄位。