springboot项目中jackson跟fastjson设置时区

 

 

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
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.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;


@Configuration
public class CorsConfig2 implements WebMvcConfigurer {

    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowedHeaders("*");
    }
    //  设置时区 (TimeZone是jdk自带的时区类,这种方式可以用来设置fastjson、jackson的默认时区)
//    @PostConstruct
//    public void setTimeZone(){
//        TimeZone timeZone = TimeZone.getTimeZone("UTC");
//        TimeZone.setDefault(timeZone);
 //   }

//    设置时区 jackson 设置时区  系统统中就可以设置默认的序列化后格式   如想要返回 yyyy-MM-dd 只需要在
//    jsonformat 注解上加 yyyy-MM-dd
//    spring.jackson.time-zone=GMT+8
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setTimeZone(TimeZone.getDefault());
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS));
        converter.setObjectMapper(mapper);
        return converter;
    }

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


    //    fastjson 设置不了时区
//    @Bean
//    public HttpMessageConverters fastJsonConverters() {
//        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
//        HttpMessageConverter<?> httpMessageConverter = fastJsonConverter;
//        return new HttpMessageConverters(httpMessageConverter);
//    }
//
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
//        //converters.add(fastJsonConverter);//这会让fastJsonConverter排在消息转换器管道列表的最后,可能会轮不到它处理消息转换
//        converters.add(0, fastJsonConverter);//要显示指明将fastJsonConverter排在消息转换器管道列表的首位
//    }
}

 

springboot可以通过配置设置jackson时区和时间格式,参考类:org.springframework.boot.autoconfigure.jackson.JacksonProperties

例子:spring.jackson.time-zone=GMT+8

这个时区配置后,feign调用也没有问题了。

 

转载:https://blog.csdn.net/qq_36763236/article/details/111866828

 

 

 

posted @ 2022-04-22 15:00  迷走神经  阅读(3254)  评论(0编辑  收藏  举报