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}