matrix.boot.based.serializer.DateTimeSerializer Maven / Gradle / Ivy
package matrix.boot.based.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import matrix.boot.common.utils.DateUtil;
import java.io.IOException;
import java.util.Date;
/**
* 序列化Date类型为string
*
* @author wangcheng
*/
public class DateTimeSerializer extends JsonSerializer {
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (date != null) {
gen.writeString(DateUtil.format(date, DateUtil.STANDARD_FORMATTER));
} else {
gen.writeNull();
}
}
}