spring boot 2.0.6 升级(采坑)感想

spring boot 2.0.6 升级(采坑)感想

1、为什么要升级

因为我就是喜欢用最新的版本。
开个玩笑,
当你每天面对的核心代码依赖版本是几年前的时候;
当你想用的某个新的特性因为版本问题无法使用的时候;
当jdk都已经升级到了11而你的spring版本只支持到8;
当你每天看到的博客里面都说spring5各种特性的时候;
所谓兵欲善其事,必先利其器,你是否在这一刻心动了,是时候改变了,时代在进步,而我不想太落后。
也许有人说现在的版本很稳定,你升级了版本后除了问题怎么办?(一片乌鸦飞过天际....我不想回答这个问题)
那么接下来进入正题,升级的血泪史来了。。。。。。。为自己默哀28秒

2、升级概要

步骤:

1. 从官网找到最新版本:2.0.6.RELEASE
2. 找到官方Demo(https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples)ps:原来支持这么多东西,不怕不怕
3. 简单粗暴,不要怂,直接把项目里面的各种依赖升级到最新版本。ps:当然,请不要影响进展的业务,拉个新的分支出来
4. 运行项目,啪啪啪报错。。。

本次升级涉及到的版本变更如下:

1. spring boot 1.4.3.RELEASE 咚咚 2.0.6.RELEASE
2. druid-spring-boot-starter 1.1.6 咚咚 1.1.10
3. fastjson 1.2.44 咚咚 1.2.51
4. lombok 1.16.10 咚咚 1.18.2
3、坑坑坑
雷区1

数据库连不上了。快到斩乱麻,直接改配置。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306?useSSL=false&allowMultiQueries=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: username
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
雷区2

老版本

spring:
  profiles:
    #active: ${SERVER_ENVIROMENT}
    active: ${SERVER_ENVIROMENT:dev}
  mvc:
    throw-exception-if-no-handler-found: true
  resources:
    add-mappings: false
  http:
    multipart:
      max-file-size: 10MB
      max-request-size: 15MB

新版本

spring:
  profiles:
    #active: ${SERVER_ENVIROMENT}
    active: ${SERVER_ENVIROMENT:dev}
  mvc:
    throw-exception-if-no-handler-found: true
  resources:
    add-mappings: false
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 15MB
雷区3

当服务不需要加载数据源的时候。

老版本

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

新版本:

@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class})
public class MyApplication {


    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

原因:我再研究研究,时间紧迫,大家也可以自行去搜索搜索。和新版本默认使用HikariCP作为数据库连接池有关。

雷区4

直接上代码吧
老版本:

@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {}

新版本:

@Configuration
public class MyWebConfig implements WebMvcConfigurer {}

就是这么不一样

雷区5

由于雷区4当时没有发现导致我的代码自动化脚本一片飘红,原因是HttpMessageConverterSpring默认会使用MappingJackson2HttpMessageConverter,因为雷区4里面的老版本配置不生效,项目里面以前都统一用的FastJsonHttpMessageConverter作为Json转换器

雷区6(零零散散花费了我大概一天时间)

原因

* 由于项目中的自研SOA框架底层RPC的时候用的是RestTemplate。
* 我并没有自定义RestTemplate的HttpMessageConverter
* 所以RestTemplate的http请求响应解析用的是MappingJackson2HttpMessageConverter

结果

* 导致响应实体没有添加无参构造的时候报错

解决过程:

  1. 自定义RestTemplateHttpMessageConverter,去掉MappingJackson2HttpMessageConverter,加上FastJsonHttpMessageConverter
  2. 心想,这下没问题了吧,好,启动项目,what a fuck,启动不起来了。
  3. 就这个问题找了好几个小时,各种换HttpMessageConverter顺序,各种debug,就是解决不了啊,还有别的事情,哎,今天果断放弃。
  4. 第二天空一点点了,接着调。。。过程就不赘述了,也怪眼神不好,没看到以下坑B的报错:


    WechatIMG2772.jpeg
解决方案

至此,问题也定位到了,fastjson版本升级问题:Content-type变成了Content-type:*/*
。惊不惊喜,刺不刺激,上改动后的代码吧。

@Bean
    public RestTemplate getRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        List> converters = restTemplate.getMessageConverters();
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        List supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        converters.add(fastJsonHttpMessageConverter);
        restTemplate.setMessageConverters(converters);
        return restTemplate;
    }

好了,现在请求和响应实体不写默认构造参数也可以啦。

结束语

分别总是在九月,回忆是思念的愁。
感想也该结束了。
我们会持续学习,争取早日用上Spring5的新特性。

你可能感兴趣的:(spring boot 2.0.6 升级(采坑)感想)