【1】自定义时间,序列化类
- package com.fsti.bm.utils;
- import java.io.IOException;
- import java.util.Date;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.JsonSerializer;
- import com.fasterxml.jackson.databind.SerializerProvider;
- /**
- * 自定义时间,序列化类
- *
- * yyyy-MM-dd hh:mm:ss
- *
- * @author deniro
- */
- public class CustomDateSerializer extends JsonSerializer<Date> {
- @Override
- public void serialize(Date value, JsonGenerator jgen,
- SerializerProvider provider) throws IOException,
- JsonProcessingException {
- jgen.writeString(DateUtils.toStr(value));
- }
- }
【2】POJO添加序列化注解
- import com.fasterxml.jackson.databind.annotation.JsonSerialize;
- import com.fsti.bm.utils.CustomDateSerializer;
- @JsonSerialize(using = CustomDateSerializer.class)
注意:该类必须实现 java.io.Serializable
如果使用hibernate,则hbm配置文件相应的字段类型,必须是 type="timestamp"