问题
请求接口返回的日期参数总是毫秒值,但是我需要的是这种格式:2019-11-07 15:35:48
项目概况
- spring boot2.2.0
- 使用了实现 WebMvcConfigurer接口的拦截器
试了好几种方法
@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;
@Configuration @Slf4j public class WebConfig implements WebMvcConfigurer { public HttpMessageConverter<String> stringConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8")); return converter; }
public HttpMessageConverter fastConverter() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty); fastJsonConfig.setCharset(Charset.forName("UTF-8")); List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); return fastConverter; } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.clear(); converters.add(fastConverter()); converters.add(stringConverter()); }
@Override public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CorsInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new TokenHandlerInterceptor()).addPathPatterns("/common/**") .excludePathPatterns("/swagger-ui.html/**"); } }
|
现在在这里加上这一行就可以了
1
| SerializerFeature.WriteDateUseDateFormat,
|