Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report