日期字段序列化
一、示例代码
注意:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") 若不指明时区,反序列化的时候可能会出现真实时间 +8 小时的情况出现。
import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Builder; import lombok.Getter; import lombok.Setter; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; import java.util.Map; /** * @author xxx */ @Getter @Builder @ToString public class Entry implements IAlertMsg { private String url; //map类型的成员变量,json返序列化时使用set方法 @Setter private Map<String, String> strValues; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date timeStamp; @Tolerate public Entry() {} }
二、需要的maven包
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.8.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>3.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>