踩坑-spring boot2.2.0返回json日期格式問題

問題:

  請求接口返回的日期參數總是毫秒值,但是我需要的是這種格式:2019-11-07 15:35:48

項目概況:

  •   spring boot2.2.0
  • 使用了實現 WebMvcConfigurer接口的攔截器

試了好幾種方法:

  • @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss", timezone = "GMT+8"),沒有效果
  •  
spring.jackson.date-format=yyyy-MM-dd
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false

沒有效果

 

因爲我使用了WebMvcConfigurer攔截器影響到了序列化日期格式

原來的代碼WebMvcConfigurer配置代碼

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/**");
	}
}

現在在這裏加上這一行就可以了

SerializerFeature.WriteDateUseDateFormat,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章