Scenario: The type of the entity class is the date type, and nothing is done. The return to the front end is a timestamp, but there are more than 3 0s in the back, because the front end wants seconds, and the passed is milliseconds, we need Deal with it.
Need to use @JsonSerialize annotation
defined tools
package com.imooc.untis.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;
/**
* Created by 敲代码的卡卡罗特
* on 2018/3/3 22:01.
*/
public class Date2LongSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeNumber(date.getTime() / 1000);
}
}
Then add this annotation on the entity class
/** 创建时间. */
@JsonSerialize(using = Date2LongSerializer.class)
private Date createTime;
/** 更新时间. */
@JsonSerialize(using = Date2LongSerializer.class)
private Date updateTime;