Precautions for spring boot 1.x up 2.x

Keywords: Programming Spring Redis Mobile Thymeleaf

Record some errors during upgrade.

This upgrade is 1.5.x upgrade to 2.1.4

----------------------------------------------

If java version is lower than 8, please do not upgrade. spring boot 2.0 requires at least java 8.

    ----------------------------------------------

    One: WebMvcConfigurerAdapter has been abandoned

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedMethods("GET", "POST", "PATCH", "DELETE");
        }
        
        @Bean
        public Filter characterEncodingFilter() {
            CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding("UTF-8");
            characterEncodingFilter.setForceEncoding(true);
            return characterEncodingFilter;
        }
       
        @Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		//Add mobile terminal interceptor
    		registry.addInterceptor(new UserRoleInterceptor()).addPathPatterns("/app/**");
    		super.addInterceptors(registry);
    	}
    
    
    	 /**  
    	 * @Title: multipartConfigElement  
    	 * @Description: Control image upload size  
    	 * @return  
    	 */
    	@Bean
    	 public MultipartConfigElement multipartConfigElement() {
            MultipartConfigFactory factory = new MultipartConfigFactory();
            factory.setMaxFileSize(1024L * 1024L * 8);
            return factory.createMultipartConfig();
    	 }
    
    }

    2.0 needs to inherit the WebMvcConfigurer interface

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedMethods("GET", "POST", "PATCH", "DELETE");
        }
        
        @Bean
        public Filter characterEncodingFilter() {
            CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding("UTF-8");
            characterEncodingFilter.setForceEncoding(true);
            return characterEncodingFilter;
        }
    
        @Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		//Add mobile terminal interceptor
    		registry.addInterceptor(new UserRoleInterceptor()).addPathPatterns("/mobile/**");
    	}
    
    	 /**  
    	 * @Title: multipartConfigElement  
    	 * @Description: Control image upload size  
    	 * @return  
    	 */
    	@Bean
    	 public MultipartConfigElement multipartConfigElement() {
            MultipartConfigFactory factory = new MultipartConfigFactory();
            factory.setMaxFileSize(DataSize.ofBytes(1024L * 1024L * 8));
            return factory.createMultipartConfig();
    	 }
    }

     

    II. Change of time stamp

    When springboot1.x is requested, the Date type is converted to time stamp by default. After springboot2.0, the time format returned by default becomes UTC string:

    spring:
      jackson:
        time-zone: GMT+8
        serialization:
          write-dates-as-timestamps: true

     

    Third: the change of redis

    1.x uses jedis by default, and lettuce is used in versions above 2.0.

    In addition, a data is added to the redis dependency Name: spring boot starter data redis

    spring:
      redis:
        host: xxx.xxx.xxx.xxx
        database: 0
        port: 6379
        password: xxxxx
        lettuce:
          pool:
            max-idle: 8
            min-idle: 2
            max-active: 50
            # Connection timeout in ms
            max-wait: 8000ms

     

    Four: spring cloud depends on the pit

    There are too many versions of spring cloud, which supports the above versions will not be described here. I used a relatively new version: Greenwich.SR3. This version is mainly designed to rely on name changes,

    As shown in the figure below, pay attention to the prompt, just find the changed version

     

    Summary:

    <!--spring-cloud-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
    
    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-hystrix</artifactId>
            </dependency>

    In addition, there is another pit in Fegin that needs to be configured with allow bean definition overriding to solve:

    spring:
      main:
        allow-bean-definition-overriding: true

       

    V. compact configuration file

    In many cases of yml configuration, hump writing cannot be used. When you start, you will be prompted to use "-" to split letters. All letters are in lowercase. This was found when upgrading druid, because many custom configurations have been written before.

     

    Vi. update of other components

    Due to the use of spring 5.x, other third-party packages that rely on the lower version of spring should be noted. If they are not directly related to spring boot, they need to be upgraded synchronously.

    Such as:

    <artifactId>thymeleaf-spring4</artifactId>

    Need to upgrade to:

    <! -- this is just an example. The best way to avoid this is to introduce the spring boot related thmeleaf -- >
    <artifactId>thymeleaf-spring5</artifactId>

    Spring Boot 2.0 has updated its component support, and the supported components are as follows:

    • Upgrade Tomcat to 8.5
    • Flyway upgrade to 5
    • Hibernate upgrade to 5.2
    • Thymeleaf upgrade to 3
       

    Seven: jackson conflict:

    ObjectMapper is not found.

    <!--Please be sure to exclude jackson-annotations,Avoid package conflicts-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-annotations</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </dependency>
    

    Posted by flying_circus on Mon, 18 Nov 2019 00:57:01 -0800