标签:div fas str strong res created code 简介 enum
简介:介绍常用json框架和注解的使用,自定义返回json结构和格式
1、常用框架 阿里 fastjson,谷歌gson等
JavaBean序列化为Json,性能:Jackson > FastJson > Gson > Json-lib 同个结构
Jackson、FastJson、Gson类库各有优点,各有自己的专长
空间换时间,时间换空间
2、jackson处理相关自动
指定字段不返回:@JsonIgnore
指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
空字段不返回:@JsonInclude(Include.NON_NUll)
指定别名:@JsonProperty
实体类代码如下:
public class rData { @JsonIgnore private String code; @JsonProperty(value = "agenum") private int age; @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8") private Date createDate; @JsonInclude(JsonInclude.Include.NON_NULL) private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public rData(String code, int age) { this.code = code; this.age = age; } public rData(String code, int age, Date createDate, String name) { this.code = code; this.age = age; this.createDate = createDate; this.name = name; } }
测试类代码:
@RestController public class HttpController { @GetMapping("/testjson") public Object param6(){ return new rData("jackson",1,new Date(),"lion"); } }
结果:
{"createDate":"2018-09-18 09:36:31","name":"lion","agenum":1}
code被忽略了,所以不显示;age被别名代替"agenum"
标签:div fas str strong res created code 简介 enum
原文地址:https://www.cnblogs.com/Mblood/p/9667191.html