影落离风

The shadow falls away from the wind

0%

踩坑-spring boot2.2.0返回json日期格式问题

问题

请求接口返回的日期参数总是毫秒值,但是我需要的是这种格式:2019-11-07 15:35:48

项目概况

  • spring boot2.2.0
  • 使用了实现 WebMvcConfigurer接口的拦截器

试了好几种方法

  1. @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss", timezone = "GMT+8"),//没有效果
    <!--0-->
    

因为我使用了WebMvcConfigurer拦截器影响到了序列化日期格式

原来的代码WebMvcConfigurer配置代码

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cxd.tool.filter.TokenHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
* 请求与返回参数处理
*
* @author MinWeikai
* @date 2019/6/24 10:57
*/
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

public HttpMessageConverter<String> stringConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
}

/**
* 格式化返回体,去除null为空字符串
*
* @return
*/
public HttpMessageConverter fastConverter() {
//1、定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
//字符串null返回空字符串
SerializerFeature.WriteNullStringAsEmpty,
//数值类型为0
SerializerFeature.WriteNullNumberAsZero,

//空字段保留
SerializerFeature.WriteNullListAsEmpty);

fastJsonConfig.setCharset(Charset.forName("UTF-8"));
//2-1 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
return fastConverter;
}

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.clear();
converters.add(fastConverter());
converters.add(stringConverter());
}

/**
* 请求拦截处理
* addPathPatterns 用于添加拦截规则
* excludePathPatterns 用于排除拦截
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 跨域拦截器需放在最上面
* 解決H5页面OPTIONS问题
*/
registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**");
//业务拦截处理
registry.addInterceptor(new TokenHandlerInterceptor()).addPathPatterns("/common/**")
.excludePathPatterns("/swagger-ui.html/**");
}
}

现在在这里加上这一行就可以了

1
SerializerFeature.WriteDateUseDateFormat,