例子:
1.将Date类型从毫秒级变为秒级的:
a.编写一个继承JsonSerializer的类:
public class Data2LongSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeNumber(date.getTime()/1000); } }
b.在返回页面的VO对象中添加注解
/** 创建时间. */ @JsonSerialize(using = Data2LongSerializer.class) private Date createTime; /** 更新时间. */ @JsonSerialize(using = Data2LongSerializer.class) private Date updateTime;
2.若属性为null则不返回
a.在返回页面的VO对象添加注解(仅在单一返回页面的VO对象生效):
@Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO {...}
b.在application.yml中设置(全局配置):
jackson:
default-property-inclusion: non_null