json序列化将Date类型转为TimeStamp返回

新建序列化类:Date2TimeStampSerializer

public class Date2TimeStampSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        if(date==null){
           jsonGenerator.writeObject(null);
        }else{
            jsonGenerator.writeNumber(date.getTime() );
        }
    }
}

使用:

@JsonSerialize(using = Date2TimeStampSerializer.class)
private Date createTime;

date为null返回null,不为null返回时间戳

发表新评论