• java返回前端实体类json数据时忽略某个属性方法


    使用情景

    日常工作里经常会碰到需要隐藏实体类的某些属性,可以减少返回值的大小,对性能可以有一定的提高,比如像create_time,update_time,is_del等等一些属性没有必要返回前端的。

    使用方法

    方法一

    SpringBoot中忽略实体类中的某个属性不返回给前端的方法:使用Jackson的方式://第一种方式,使用@JsonIgnore注解标注在属性上。

    1. public class HsUser {
    2. private Long id;
    3. private Long customerId;
    4. private String customerName;
    5. private Integer status;
    6. @JsonIgnore
    7. private Integer isDel;
    8. }

    方法二

    使用@JsonIgnoreProperties标注在类上,可以忽略指定集合的属性

    1. @JsonIgnoreProperties({"isDel"})
    2. public class HsUser {
    3. private Long id;
    4. private Long customerId;
    5. private String customerName;
    6. private Integer status;
    7. private Integer isDel;
    8. }

    方法三

    使用fastjson时:使用@JSONField(serialize = false)注解

    1. public class HsUser {
    2. private Long id;
    3. private Long customerId;
    4. private String customerName;
    5. private Integer status;
    6. @JSONField(serialize = false)
    7. private Integer isDel;
    8. }

    方法四

    加上 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) :前端就不能接收到

    1.     @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    2.     private String password;

     方法五

    如果是null不返回,注解:@JsonInclude(value= JsonInclude.Include.NON_NULL) 返回的字段属性为null 就不会展示给前端...可以放在类上,也可以放在字段上!

    1. @JsonInclude(value= JsonInclude.Include.NON_NULL)
    2. public class HsUser {
    3. private Long id;
    4. private Long customerId;
    5. private String customerName;
    6. private Integer status;
    7. private Integer isDel;
    8. }

  • 相关阅读:
    numpy PIL tensor之间的相互转换
    Doris数据库使用记录
    算法之双指针
    【C语言】.c源文件从编译到链接生成可.exe执行程序的过程
    作业-11.24
    Bean的四种实例化方式以及BeanFactory和FactoryBean的区别
    Mac电脑配置Tomcat
    IDEA回滚代码
    【C++和数据结构】位图和布隆过滤器
    物联网:SpringBoot 集成Websocket 前后端客户端 及 mqtt 实现设备联动
  • 原文地址:https://blog.csdn.net/m0_73774439/article/details/139967827