问题由来:
今天看大神网上发布的一个开源项目,看到使用了这个注解,不甚了解百度了一下。

springboot项目中定义了很多类,我们在rest返回中直接返回或者在返回对象中使用这些类,spring已经使用jackson自动帮我们完成这些的to json。但是有时候自动转的json内容太多,或者格式不符合我们的期望,因此需要调整类的to json过程,或者说希望自定义类的json过程。

解决办法:
使用@JsonIgnoreProperties、@JsonIgnore、@JsonFormat。

@JsonIgnore注解用来忽略某些字段,可以用在变量或者Getter方法上,用在Setter方法时,和变量效果一样。这个注解一般用在我们要忽略的字段上。

@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段。这个注解还可以指定要忽略的字段,例如@JsonIgnoreProperties({ “password”, “secretKey” })

@JsonFormat可以帮我们完成格式转换。例如对于Date类型字段,如果不适用JsonFormat默认在rest返回的是long,如果我们使用@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”),就返回"2018-11-16 22:58:15"

具体可以参考官方文档
https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonIgnoreProperties.html
@JsonIgnoreProperties与@JsonIgnore的主要区别在于, @JsonIgnoreProperties是类级别的, 而@JsonIgnore是变量和方法级别的。

实际代码
完整的程序在这里,欢迎加星,fork。
代码简要说明, User类的fullName 和comment字段会被@JsonIgnoreProperties注解忽略。address字段会被@JsonIgnore注解忽略。regDate会按照@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”)进行格式转。

@Data
@JsonIgnoreProperties(value = {"fullName", "comment"})
public class User {
    private String id;
    private String name;
    private String fullName;
    private String comment;
    private String mail;

@JsonIgnore
    private String address;

@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date regDate;

private Date reg2Date;
}

@Data
@JsonIgnoreProperties(value = {"fullName", "comment"})
public class User {
    private String id;
    private String name;
    private String fullName;
    private String comment;
    private String mail;

@JsonIgnore
    private String address;

@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date regDate;

private Date reg2Date;
}

我们的controller示例代码

@ApiOperation(value = "按用户id删除", notes="private")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", defaultValue = "2", value = "userID", required = true, dataType = "string", paramType = "path"),
    })
    @DeleteMapping(value = "/users/{userId}", produces = "application/json;charset=UTF-8")
    public User delUser(@PathVariable String userId) {
        User user = (User)userSvc.deleteById(userId);
        log.info("rest del user={} by id={}", user, userId);
        return user;
    }
@ApiOperation(value = "按用户id删除", notes="private")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", defaultValue = "2", value = "userID", required = true, dataType = "string", paramType = "path"),
    })
    @DeleteMapping(value = "/users/{userId}", produces = "application/json;charset=UTF-8")
    public User delUser(@PathVariable String userId) {
        User user = (User)userSvc.deleteById(userId);
        log.info("rest del user={} by id={}", user, userId);
        return user;
    }
可以看到返回的对象是User,然后comment、fullName、address属性被忽略了,regDate的格式进行转换。

@JsonIgnoreProperties、@JsonIgnore、@JsonFormat相关推荐

  1. Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释...

    转自Hibernate JPA中@Transient.@JsonIgnoreProperties.@JsonIgnore.@JsonFormat.@JsonSerialize等注解解释 1.@Tran ...

  2. Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1.@Transient @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将 ...

  3. @JsonInclude、@JsonIgnore和@JsonFormat注解

    一.@JsonIgnore: 1.作用: 在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. 一般标记在属性或者方法上,在返回的json数据就不包含该属性 2.场景模 ...

  4. jackson 的注解:@JsonProperty、@JsonIgnore、@JsonFormat 用法说明

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 导包: <dependency><groupId>com.faster ...

  5. Spring Boot程序中@JsonIgnoreProperties与@JsonIgnore基本使用

    问题由来: springboot项目中定义了很多类,我们在rest返回中直接返回或者在返回对象中使用这些类,spring已经使用jackson自动帮我们完成这些的to json.但是有时候自动转的js ...

  6. 忽略属性 @JsonIgnore、@JsonIgnoreProperties

    忽略属性 @JsonIgnore.@JsonIgnoreProperties @JsonIgnore @JsonIgnoreProperties 原文链接 @JsonIgnore @JsonIgnor ...

  7. 07【Listener、Ajax、Json】

    文章目录 07[Listener.Ajax.Json] 一.监听器 1.1 监听器的类别 1.1.1 监听域对象 1.1.2 监听属性 1.3.3 监听session对象状态 1.2 监听案例 1.2 ...

  8. Spring MVC注解@Valid、@JsonSerialize、@JsonView等

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/yuchao2015/article/details/78695954 @Valid Spring MVC采 ...

  9. SpringBoot+MongoDB GridFS文件上传、下载、预览实战

    SpringBoot + MongoDB GridFS 随着web 3.0的兴起,数据的形式不局限于文字,还有语音.视频.图片等.高效存储与检索二进制数据也成为web 3.0必须要考虑的问题.然而这种 ...

最新文章

  1. 关于consistent hash的思考及改进方案
  2. idea配置jfinal_Jfinal 项目在idea上创建
  3. 图像处理特征不变算子系列之KLT算子
  4. Dubbo3 Triple 协议简介与选型思考
  5. Could not find a version that satisfies the requirement requests (from version : )
  6. Lucence.Net学习+盘古分词
  7. 不支持所上传的文件格式。请确认选择的文件无误。_职场人必备!如何把PDF文件转换成word文档?...
  8. Windows微信双开,三开,多开小程序!start C:\“Program Files (x86)“\Tencent\WeChat\WeChat.exe
  9. 插件check_mysql_health安装和错误解决(监控Mysql状态使用)
  10. Mac电脑使用:Mac电脑如何用自带的Safari浏览器,一键翻译英文网页为中文网页
  11. Emoji表情正则匹配
  12. Spring配置文件报错 :通配符的匹配很全面, 但无法找到元素 ‘context:property-placeholder‘ 的声明
  13. android 渐变的背景色,Android背景渐变xml
  14. 类,__dift__,__len__,__add__,__new__,__init__
  15. 《微波原理与技术》学习笔记3传输线理论-史密斯圆图(Smith Chart)
  16. allergro音乐术语什么意思_rit(这是音乐术语)什么意思?
  17. Codeforces Round #764 (Div. 3)部分题解
  18. c语言中热河输入空格,热河 - 在線打字測試(dazi.kukuw.com)
  19. python数据分析 - 关联规则Apriori算法
  20. 华擎H97 Performance 1150主板板载千兆网卡手动从百兆切换千兆设置

热门文章

  1. 运营商认可5G厘米波技术,美国主推的5G毫米波技术被边缘化
  2. 漏洞分析——二进制漏洞
  3. 通用计算机中的四个模块,河南理工大学计算机嵌入式系统原题
  4. Velocity ユーザガイド
  5. 索尼android 5.0,索尼手机运行安卓5.0!太漂亮了
  6. Google Earth Engine(GEE)——河流的缓冲区
  7. python绘制双折线图
  8. ACM练级日志:带权并查集与食物链
  9. 邮件服务器类型的区别
  10. linux 安装oracle