想要在實體類中使用@JsonFormat實現日期自動格式化,註解後發現沒有起作用,查了一下才意識到@JsonFormat是隻在使用Jackson進行JSON序列化時才能起作用的,而我爲了使用Gson,將Jackson的依賴像下面這樣禁掉了,所以註解才無效的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
Spring Boot默認是用Jackson進行JSON解析和序列化的,把<exclusions>的內容去掉就可以正常使用了。