创建

package com.sheep.emo.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * @author sheep
 */
public class Date2LongSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeNumber(date.getTime() / 1000);
    }
}

使用

    /**
     * 优惠金额
     */
    private BigDecimal price;
    /**
     * 开始时间
     */
    @JsonSerialize(using = Date2LongSerializer.class)
    private Date startAt;
    /**
     * 结束时间
     */
    @JsonSerialize(using = Date2LongSerializer.class)
    private Date endAt;