日期类型,我们需要统一它在被序列化后的格式,序列化是指把java类型通过网络系统或者IO进行持久化,如把一个java对象保存到txt文件里,或者把java对象保存到redis里,又或者把java对象通过网络以JSON的格式输出到浏览器上。
jackson的日期序列化
public class JacksonDateSerializerModifier extends BeanSerializerModifier {
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
for (Object beanProperty : beanProperties) {
BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;
Class<?> clazz = writer.getType().getRawClass();
if (clazz.equals(Date.class)) {
writer.assignSerializer(new DateSerializer());
}
}
return beanProperties;
}
/**
* 格式化日期
*/
public class DateSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object date, JsonGenerator