温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何解决SpringBoot版本升级引起数据显示出错及排查

发布时间:2021-09-29 17:22:40 来源:亿速云 阅读:153 作者:柒染 栏目:大数据

如何解决SpringBoot版本升级引起数据显示出错及排查,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

描述

原来环境

Spring boot1.5.3

fastjson

<!--阿里 FastJson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

pojo中配置

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.format.annotation.DateTimeFormat;

 	@JSONField(format = "yyyy-MM-dd HH:mm")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date pubTime;

测试结果

"pubTime": "2019-02-19 13:45",

升级2.0.6测试结果

"pubTime": "2019-02-26T09:22:24.000+0000",

排查解决

经过来回更换版本等几个小时的尝试后,分析结果:Spring Boot默认采用jackson作为解析,原因可能是采用1.5.3时,WebMvcConfigurer extends WebMvcConfigurerAdapter类中关于fastjson的配置起了作用,解析框架采用了fastjson(@JSONField);而升级为2.0.6之后,由于没有对WebMvcConfigurer配置(原WebMvcConfigurerAdapter上自动加了删除线),Spring boot默认采用了jackjson解析框架,导致@JSONField未起作用,故出现上述解析结果。

解决方案

就是要自己定义解析框架fastjson,不用Spring boot默认的jackson框架。

在启动类中添加以下配置:

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;

	@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        //创建FastJson信息转换对象
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        //创建Fastjosn对象并设定序列化规则
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 中文乱码解决方案
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//设定json格式且编码为UTF-8
        fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);

        //规则赋予转换对象
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }

问题得到解决,时间格式可以正常返回显示。

看完上述内容,你们掌握如何解决SpringBoot版本升级引起数据显示出错及排查的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI