• 【SpringBoot】中bean里大写的字段返回变成小写


    例如我的bean中有以下4个字段

          private int Ret;
          private String Msg;
          private Object Data;
          private String Sig;
    

    引入依赖

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.11.3</version>
    </dependency>
    

    在返回的json里只会显示:{"ret":"xx","msg":"xx","data":"xx","sig":"xx"}

      * 使用下面方法优化完美解决该问题。
      * 返回json会变成:[{"Ret":"xx","Msg":"xx","Data":"xx","Sig":"Sig"}](www.baidu.com),完美解决问题!
      * 大小会变成小写,特殊符号开头的字段都不会显示,其原因是因为springboot在进行序列化和反序列时对字段进行了处理。
    

    解决方案是:import com.fasterxml.jackson.annotation.JsonProperty;

      * 在get方法上加上该注解@JsonIgnore
      * 在字段上加上该注解@JsonProperty
    
    package com.hlht.evcs.bean;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    /**
     * 查询数据库返回
     */
    public class HlhtRespDB {
        @JsonProperty(value = "Ret")
        private int Ret;
        @JsonProperty(value = "Msg")
        private String Msg;
        @JsonProperty(value = "Data")
        private Object Data;
        @JsonProperty(value = "Sig")
        private String Sig;
    
        public HlhtRespDB() {
        }
    
        public HlhtRespDB(int Ret, String Msg, Object Data, String Sig) {
            this.Ret = Ret;
            this.Msg = Msg;
            this.Data = Data;
            this.Sig = Sig;
        }
    
        @JsonIgnore
        public int getRet() {
            return Ret;
        }
    
        public void setRet(int ret) {
            Ret = ret;
        }
    
        @JsonIgnore
        public String getMsg() {
            return Msg;
        }
    
        public void setMsg(String msg) {
            Msg = msg;
        }
    
        @JsonIgnore
        public Object getData() {
            return Data;
        }
    
        public void setData(Object data) {
            Data = data;
        }
    
        @JsonIgnore
        public String getSig() {
            return Sig;
        }
    
        public void setSig(String sig) {
            Sig = sig;
        }
    }
    
  • 相关阅读:
    Linux下安装nginx
    MySQL基础
    win10 安装MySQL 5.7.27
    Java IO之File
    java并发编程之ThreadLocal
    原生JS 的60秒倒计时!
    vueX 配合路由导航配置动态路由
    JS获取 当前时间是本年的第几天? 第几周?
    vue书写echarts 100px大小问题
    VUE 父组件与子组件双向数据绑定的方法 做弹框绑定列表页数据的举例
  • 原文地址:https://www.cnblogs.com/Twittery/p/14299247.html
Copyright © 2020-2023  润新知