背景

数据库中字段为时间类型,页面显示全部变成了 Long 类型。

如果一个个处理会非常的麻烦。

解决方案

返回参数

1,每个实体属性添加 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") 注解

2,可以在配置文件中全局指定

  [plaintext]
1
2
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8

接受参数

1, 每个实体属性添加

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")

2, 配置文件全局配置

  [plaintext]
1
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss

配置不生效的问题

开始制定格式化,显示都好好的,后来发现不行了。

看了下原因,有的说是指定了 @EnableWebMvc,发现自己没有使用。

后来定位出原因是自己重写了 WebMvcConfigurationSupport,导致默认的配置无效。

解决方案

可以重新指定:

  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import java.text.SimpleDateFormat; import java.util.List; /** * @author binbin.hou * @since 0.0.11 */ @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 使用此方法, 以下 spring-boot: jackson时间格式化 配置 将会失效 * spring.jackson.time-zone=GMT+8 * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss * 原因: 会覆盖 @EnableAutoConfiguration 关于 WebMvcAutoConfiguration 的配置 * */ @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = converter.getObjectMapper(); // 生成JSON时,将所有Long转换成String SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); // 时间格式化 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); // 设置格式化内容 converter.setObjectMapper(objectMapper); converters.add(0, converter); } }

参考资料

springboot 格式化日期

SpringBoot中spring.jackson.date-format配置失效的解决办法

解决springboot配置jackson.date-format不生效的问题