SpringBoot2.X学习总结:(2)json框架和注解

1、常用框架fastjson,gson等

    JavaBean序列化Json,性能:Jackson->FastJson->Gson->Json-Lib

2、jackson处理相关

   1)指定字段不返回:@JsonIgnore

    2)指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")

    3)空字段不返回:@JsonInclude(JsonInclude.Include.NON_NULL)

    4)指定别名:@JsonProperty

3、bean类

public class User {
    @JsonProperty("userid")
    private int id;
    @JsonIgnore
    private String pwd;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String phone;
    @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
    private Date createTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public User(int id, String pwd, String phone, Date createTime) {
        this.id = id;
        this.pwd = pwd;
        this.phone = phone;
        this.createTime = createTime;
    }
}

4、controller调用

@GetMapping("/res/testjson")
public Object testJson(){
  return new User(1,"123456",null,new Date());
}

结果
{"createTime":"2018-08-18 10:39:27","userid":1}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值