常用框架
阿里 fastjson
谷歌 gson
JavaBean序列化Json,性能:Jackson > FastJson > Gson > Json-lib
Jackson处理相关
指定字段不返回: @JsonIgnore
指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss", locale="zh",timezone="GMT+8")
空字段不返回: @JsonInclude(Include.NON_NULL)
指定别名: @JsonProperty
例子1
public class User {
private int age;
@JsonProperty("mingzi")
private String name;
@JsonIgnore
private String pwd;
@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss", locale="zh",timezone="GMT+8")
private Date createTime;
public User(int age, String name, String pwd, Date createTime) {
this.age = age;
this.name = name;
this.pwd = pwd;
this.createTime = createTime;
}
...
}
@RestController
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello word!";
}
@GetMapping("/testjson")
public Object testjson() {
return new User(101, "admin", "admin123", new Date());
}
}