关于后端带下划线的变量名转Json格式返回给前端时缺少下划线的bug

1.问题分析

代码:

  • IpsegmentVo
package com.sccl.modules.data.ipsegment.domain;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by wangbin
 * 2019-08-14-19:21
 */
public class IpsegmentVo extends Ipsegment {
    private String planName;// 关联的应用名称
    private Long statusValue; // 状态值
    private Long runStatusValue; // 业务状态
    private Long readyStatusValue; // 预占状态
    private int childrenCount; // 子节点个数
    private Integer autoBasicMsg;//是否自动生成基本信息
    private List children = new ArrayList<>();//子节点
    private Boolean _checked = false;//选中当前项(true:是,false:否)
    private Boolean _disabled = false;//禁止选择当前项(true:是,false:否)

    public Boolean get_checked() {
        return _checked;
    }

    public void set_checked(Boolean _checked) {
        this._checked = _checked;
    }

    public Boolean get_disabled() {
        return _disabled;
    }

    public void set_disabled(Boolean _disabled) {
        this._disabled = _disabled;
    }

    public Integer getAutoBasicMsg() {
        return autoBasicMsg;
    }

    public void setAutoBasicMsg(Integer autoBasicMsg) {
        this.autoBasicMsg = autoBasicMsg;
    }

    public String getPlanName() {
        return planName;
    }

    public void setPlanName(String planName) {
        this.planName = planName;
    }

    public Long getStatusValue() {
        return statusValue;
    }

    public void setStatusValue(Long statusValue) {
        this.statusValue = statusValue;
    }

    public Long getRunStatusValue() {
        return runStatusValue;
    }

    public void setRunStatusValue(Long runStatusValue) {
        this.runStatusValue = runStatusValue;
    }

    public Long getReadyStatusValue() {
        return readyStatusValue;
    }

    public void setReadyStatusValue(Long readyStatusValue) {
        this.readyStatusValue = readyStatusValue;
    }

    public int getChildrenCount() {
        return childrenCount;
    }

    public void setChildrenCount(int childrenCount) {
        this.childrenCount = childrenCount;
    }

    public List getChildren() {
        return children;
    }

    public void setChildren(List children) {
        this.children = children;
    }
}
带下划线的字段
  • IpsegmentController
//节选部分
package com.sccl.modules.data.ipsegment.controller;

import java.io.UnsupportedEncodingException;
import java.util.*;

import com.sccl.modules.data.ipsegment.domain.IpsegmentSplitVo;
import com.sccl.modules.data.ipsegment.domain.IpsegmentVo;
import com.sccl.modules.topo.element.domain.QueryData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.sccl.framework.aspectj.lang.annotation.Log;
import com.sccl.framework.aspectj.lang.constant.BusinessType;
import com.sccl.modules.data.ipsegment.domain.Ipsegment;
import com.sccl.modules.data.ipsegment.service.IIpsegmentService;
import com.sccl.common.web.controller.BaseController;
import com.sccl.framework.web.page.TableDataInfo;
import com.sccl.framework.web.domain.AjaxResult;
import com.sccl.common.support.Convert;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;

/**
 * IP地址段 信息操作处理
 * 
 * @author txb
 * @date 2019-08-14
 */
@RestController
@RequestMapping("/data/ipsegment")
public class IpsegmentController extends BaseController
{
    private String prefix = "data/ipsegment";
    
    @Autowired
    private IIpsegmentService ipsegmentService;
/**
     * 方法说明:在规划管理中对IP段进行选择时,下拉框IP段的查询
     * @author wangbin
     * @date 2019/9/27 15:26
     * @param map
     * @return com.sccl.framework.web.page.TableDataInfo
     * @throws
     */
    @RequiresPermissions("data:ipsegment:list")
    @PostMapping("/findIp4Editor")
    @ResponseBody
    public AjaxResult findIp4Editor(@RequestBody Map map) {
        System.out.println(map.get("topoGraphKind"));
        System.out.println(map.get("planType"));
        System.out.println(map.get("planId"));
        List list = ipsegmentService.findIp4Editor(map);
        System.out.println(list);
        return AjaxResult.success(list);
    }
  • 调试模式:

查看后台的返回值

带下划线的变量被成功赋值

查看前端页面

前端状态显示不正确

查看请求结果


查看请求返回的结果

返回结果中,带下划线的变量,下划线丢失

发现返回结果中,带下划线的变量,下划线丢失

正是由于返回的结果中变量名丢失下划线,前端需要的参数对应不上,导致前端页面显示的状态不正确

2.解决办法,很明显是后台返回的数据在进行json序列化的时候,带下划线的变量名被去除了下划线,由此我们只需要在带下划线的变量名上指定json序列化的名称即可

解决方案:在带下划线的变量名上使用@JSONField(name = " ")注解,指定Json序列化时,该变量的名称

代码:

  • IpsegmentVo
package com.sccl.modules.data.ipsegment.domain;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by wangbin
 * 2019-08-14-19:21
 */
public class IpsegmentVo extends Ipsegment {
    private String planName;// 关联的应用名称
    private Long statusValue; // 状态值
    private Long runStatusValue; // 业务状态
    private Long readyStatusValue; // 预占状态
    private int childrenCount; // 子节点个数
    private Integer autoBasicMsg;//是否自动生成基本信息
    private List children = new ArrayList<>();//子节点
    @JSONField(name = "_checked") //在属性的setter和getter方法上使用注解@JSONField(name="_id"),为其指定要序列化和反序列化后的属性名,getter是序列化为json,setter是反序列化为java对象。通过此注解解决转json时,变量名下划线丢失的情况
    private Boolean _checked = false;//选中当前项(true:是,false:否)
    @JSONField(name = "_disabled")
    private Boolean _disabled = false;//禁止选择当前项(true:是,false:否)

    public Boolean get_checked() {
        return _checked;
    }

    public void set_checked(Boolean _checked) {
        this._checked = _checked;
    }

    public Boolean get_disabled() {
        return _disabled;
    }

    public void set_disabled(Boolean _disabled) {
        this._disabled = _disabled;
    }

    public Integer getAutoBasicMsg() {
        return autoBasicMsg;
    }

    public void setAutoBasicMsg(Integer autoBasicMsg) {
        this.autoBasicMsg = autoBasicMsg;
    }

    public String getPlanName() {
        return planName;
    }

    public void setPlanName(String planName) {
        this.planName = planName;
    }

    public Long getStatusValue() {
        return statusValue;
    }

    public void setStatusValue(Long statusValue) {
        this.statusValue = statusValue;
    }

    public Long getRunStatusValue() {
        return runStatusValue;
    }

    public void setRunStatusValue(Long runStatusValue) {
        this.runStatusValue = runStatusValue;
    }

    public Long getReadyStatusValue() {
        return readyStatusValue;
    }

    public void setReadyStatusValue(Long readyStatusValue) {
        this.readyStatusValue = readyStatusValue;
    }

    public int getChildrenCount() {
        return childrenCount;
    }

    public void setChildrenCount(int childrenCount) {
        this.childrenCount = childrenCount;
    }

    public List getChildren() {
        return children;
    }

    public void setChildren(List children) {
        this.children = children;
    }
}

通过 @JSONField注解指定了带下划线字段转json格式时的名称

查看请求结果:


带下划线的变量按照设置的名称转json成功
返回的数据中,下划线变量的按照指定的名称转换成功,没有丢失下划线

前端页面:


勾选状态表示正确

参考文章:
https://blog.csdn.net/kisssun0608/article/details/48289535

你可能感兴趣的:(关于后端带下划线的变量名转Json格式返回给前端时缺少下划线的bug)