spring boot @ResponseBody轉換JSON 時 Date 類型處理方法,Jackson和FastJson兩種方式,springboot 2.0.9配置fastjson不生效官方解決

spring boot @ResponseBody轉換JSON 時 Date 類型處理方法 ,這裏一共有兩種不一樣解析方式(Jackson和FastJson兩種方式,springboot我用的1.x的版本)

第一種方式:默認的json處理是 jackson 也就是對configureMessageConverters 沒作配置時html

  mybatis數據查詢返回的時間,是一串數字,如何轉化成時間。兩種方法,推薦第一種java

  方法一:spring

  能夠在apllication.property加入下面配置就能夠json

  #時間戳統一轉換
  spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring-mvc

  spring.jackson.time-zone=GMT+8springboot

 

  方法二:mybatis

  @JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")mvc

  private Date createTime;ide

 

第二種方式:當configureMessageConverters 配置爲FasJson處理時;spring-boot

  方法一:全局配置:    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );

       //此處是全局處理方式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

        fastConverter.setFastJsonConfig(fastJsonConfig);

        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.ALL); // 所有格式
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);
        converters.add(fastConverter);
    }    
}

  

  方法二:在所須要的字段上配置(比較靈活的方式,根據不一樣需求轉換):

  @JSONField(format="yyyyMMdd")

  private Date createTime;

說明:這裏若是字段和全局都配置了 ,最後是以全局轉換

 

重要補充:

當springboot版本是2.0.9以上配置fastjson不生效解決以下:

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;

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

@Configuration
public class MyConfiguration {

    @Bean
    public HttpMessageConverters customConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 建立配置類
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );

        //此處是全局處理方式
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setCharset(Charset.forName("UTF-8"));
        fastConverter.setFastJsonConfig(config);

        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.ALL);
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);
        //支持text 轉string
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        return new HttpMessageConverters(fastConverter, stringHttpMessageConverter);
    }

 

 參考spring官網文檔:https://docs.spring.io/spring-boot/docs/2.0.9.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-message-converters

相關文章
相關標籤/搜索