@JsonIgnore、@JsonFormat 用法:
public class User {
@JsonIgnore//默认是true,与@JsonIgnore(true)同义,序列化时忽略该属性
private Integer id;
@JsonIgnore(value = false)//序列化时不忽略该属性
private String name;
private Double grade;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")//日期序列化时转化为该格式
private Date birthday;
public User(Integer id, String name, Double grade, Date birthday) {
this.id = id;
this.name = name;
this.grade = grade;
this.birthday = birthday;
}
//get...set...
}
User user1 = new User(101, "张三", 89.52135, new Date());
ObjectMapper mapper = new ObjectMapper();
//User类转JSON
String json = mapper.writeValueAsString(user1);
输出:
{"name":"张三","grade":89