• 【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;
        }
    }
    
  • 相关阅读:
    如何改变Activity在当前任务堆栈中的顺序,Intent参数大全
    SQL删除重复记录,并只保留一条
    SpringCloud+Eureka+Feign+Ribbon+zuul的简化搭建流程和CRUD练习
    Spring Cloud Bus 消息总线
    Spring Cloud之Swagger集群搭建
    nginx-ZUUL集群
    spring boot swagger-ui.html 404
    jenkins 部署docker 容器 eureka 集群 完整配置 多台服务器
    Linux(Centos)之安装Nginx及注意事项
    Idea 导出Modules =>jar
  • 原文地址:https://www.cnblogs.com/Twittery/p/14299247.html
Copyright © 2020-2023  润新知