FastJSON和Jackson注解

1、时间格式化注解

  • Jackson- @JsonFormat
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
  • FastJSON - @JSONField
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

2、忽略字段注解

放在字段上或get方法上都可

  • Jackson- @JsonIgnore
    @JsonIgnore
    private LocalDateTime createTime;

	@JsonIgnore
    public LocalDateTime getCreateTime() {
        return createTime;
    }
  • FastJSON - @JSONField
    @JSONField(serialize = false)
    private LocalDateTime createTime;

	@JSONField(serialize = false)
    public LocalDateTime getCreateTime() {
        return createTime;
    }

注意:
1)SpringBoot中 @RestController@ResponseBody 默认使用的是 Jackson进行JSON序列化;
2)JSON序列化时,实际是调用实体类的 get() 方法获取数据;

3、映射别名

这样在使用JSON.parseObject()的时候, 就能将access_token字段赋值到accessToken,避免手动set

  • FastJSON
@JSONField(name = "access_token")
private String accessToken;
发布了10 篇原创文章 · 获赞 3 · 访问量 294

猜你喜欢

转载自blog.csdn.net/AWen_Jack/article/details/103427547