Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve data display errors and troubleshooting caused by SpringBoot version upgrade

Shulou Source: shulou.com Published: 2022-06-02 03:36:56 09月12日 Update

How to solve the SpringBoot version upgrade caused by data display errors and troubleshooting, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Describe the original environment

Spring boot1.5.3

Fastjson

Configure 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 in com.alibaba fastjson 1.2.47 pojo Test results "pubTime": "2019-02-19 13:45", upgrade 2.0.6 Test results "pubTime": "2019-02-26T09:22:24.000+0000", troubleshooting solution

After several hours of trying to change the version back and forth, the analysis result: Spring Boot uses jackson as resolution by default, probably because when 1.5.3 is adopted, the configuration of fastjson in the WebMvcConfigurer extends WebMvcConfigurerAdapter class plays a role, and the parsing framework uses fastjson (@ JSONField). After upgrading to 2.0.6, the above resolution result occurs because there is no configuration for WebMvcConfigurer (the original WebMvcConfigurerAdapter is automatically added with a delete line), and Spring boot uses the jackjson resolution framework by default, resulting in @ JSONField not working.

Solution

It is necessary to define your own parsing framework fastjson instead of Spring boot's default jackson framework.

Add the following configuration to the startup class:

Import org.springframework.boot.autoconfigure.http.HttpMessageConverters;import org.springframework.context.annotation.Bean;import org.springframework.http.MediaType; @ Bean public HttpMessageConverters fastJsonHttpMessageConverters () {/ / create FastJson information conversion object FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter (); / / create Fastjosn object and set serialization rules FastJsonConfig fastJsonConfig = new FastJsonConfig (); fastJsonConfig.setSerializerFeatures (SerializerFeature.PrettyFormat) / / Chinese garbled solution List mediaTypes = new ArrayList (); mediaTypes.add (MediaType.APPLICATION_JSON_UTF8); / / set the json format and encode it as UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes (mediaTypes); / / assign rules to the conversion object fastJsonHttpMessageConverter.setFastJsonConfig (fastJsonConfig); return new HttpMessageConverters (fastJsonHttpMessageConverter);}

The problem has been solved and the time format can be returned to display normally.

After reading the above, do you know how to solve the data display errors and troubleshooting caused by SpringBoot version upgrade? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Upgrade framework result configuration version object problem data function content reason scheme method more format rule solution test helpless Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Shulou Tech Info Shulou Information Huawei