想要在实体类中使用@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>的内容去掉就可以正常使用了。