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 upgrade Spring Boot1.5X to 2.0

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to upgrade Spring Boot1.5X to 2.0 related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have something to gain after reading this Spring Boot1.5X how to upgrade to 2.0 article, let's take a look.

Preface

Rely on JDK version to upgrade

2.x needs at least the support of JDK 8, and many of the methods in 2.x apply many advanced new features of JDK 8, so if you want to upgrade to version 2.0, make sure your application must be compatible with JDK 8.

In addition, 2.x started support for JDK 9.

Third-party class library upgrade

2.x upgraded all the stable versions of the third-party class library that can be upgraded, and I listed some class library upgrades that are worth paying attention to.

1) Spring Framework 5 +

2) Tomcat 8.5 +

3) Flyway 5 +

4) Hibernate 5.2 +

5) Thymeleaf 3 +

Startup class reports an error

Question:

Startup class SpringBootServletInitializer red error, the imported class is not correct.

Reason:

When SpringBoot is deployed to Tomcat to start, you need to add SpringBootServletInitializer,2.0 to the startup class.

Solution:

Package com.dudu

Import com.dudu.util.MyMapper

Import org.mybatis.spring.annotation.MapperScan

Import org.springframework.boot.SpringApplication

Import org.springframework.boot.autoconfigure.SpringBootApplication

Import org.springframework.boot.builder.SpringApplicationBuilder

Import org.springframework.boot.web.servlet.support.SpringBootServletInitializer

Import org.springframework.transaction.annotation.EnableTransactionManagement

Import javax.sql.DataSource

@ SpringBootApplication

/ / enable annotation transaction management

@ EnableTransactionManagement / / enable annotated transaction management, which is equivalent to xml configuration

@ MapperScan (basePackages = "com.dudu.dao", markerInterface = MyMapper.class)

Public class Application extends SpringBootServletInitializer {

@ Override

Protected SpringApplicationBuilder configure (SpringApplicationBuilder application) {

Return application.sources (Application.class)

}

Public static void main (String [] args) {

SpringApplication.run (Application.class, args)

}

}

Configuration file error

Question:

Project name configuration error in configuration file: server.context-path: / spring

Reason:

A large number of Servlet-specific server.* properties have been moved to server.servlet:

From this we can see some clues, that is, server is no longer just servlet, there are others to join.

Solution:

Server.context-path: / spring can be changed to server.servlet.context-path: / spring

Web Starter as a transitive dependency

Question:

The template used in the project is thymeleaf, and the startup error message cannot find spring-boot-starter-web.

Reason:

In the past, there were several Spring Boot starter that relied on Spring MVC to pass the spring-boot-starter-web. With Spring WebFlux's new support, spring-boot-starter-mustache,spring-boot-starter-freemarker and spring-boot-starter-thymeleaf are no longer dependent on it. Developers are responsible for selecting and adding spring-boot-starter-web or spring-boot-starter-webflux.

Solution:

You can import spring-boot-starter-web.

Org.springframework.boot

Spring-boot-starter-web

Thymeleaf 3.0 does not include layout modules by default

Question:

When starting the project, I found that the home page was blank, and there was no error message in the background.

Reason:

The spring-boot-starter-thymeleaf package in Spring Boot 2.0 does not contain layout modules by default and is added separately when you need to use it.

Solution:

Nz.net.ultraq.thymeleaf

Thymeleaf-layout-dialect

Interceptors are out of date

Question:

WebMvcConfigurerAdapter prompts for obsolescence after upgrade

Reason:

The upgraded springBoot uses the feature default method of java8, so you can directly implement the interface WebMvcConfigurer.

Solution:

Old:

Public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter

New:

Public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer

Static resources are intercepted

Question:

The login style is not loaded when accessing the system

Reason:

In version 1.5, META-INF/resources / resources / static / public is where spring boot believes that static resources should be placed, and will automatically find static resources. In spring boot 2.0, static resources are also intercepted. When the interceptor intercepts the request, but there is no corresponding request in controller, the request will be regarded as a request for static resources. At this point, handler is ResourceHttpRequestHandler, and the above error will be thrown.

Solution:

The solution is to exclude the request path for static resources from the interceptor

/ * *

* interceptor

* @ param registry

, /

@ Override

Public void addInterceptors (InterceptorRegistry registry) {

/ / addPathPatterns is used to add intercept rules

/ / excludePathPatterns users exclude blocking

Registry.addInterceptor (new MyInterceptor ()) .addPathPatterns ("/ *") .excludePathPatternss ("/ toLogin", "/ login", "/ assets/**", "/ js/**")

}

Assets is the directory where I put static files.

Special handling of global exceptions

Question:

There are some errors mentioned in the previous article that you may want to deal with specially. Now the corresponding code is marked red and the corresponding class cannot be found.

Reason:

After the new version, this method has been removed and needs to be replaced with a new method.

Solution:

Old code:

@ Configuration

Public class ContainerConfig {

@ Bean

Public EmbeddedServletContainerCustomizer containerCustomizer () {

Return new EmbeddedServletContainerCustomizer () {

@ Override

Public void customize (ConfigurableEmbeddedServletContainer container) {

Container.addErrorPages (new ErrorPage (HttpStatus.INTERNAL_SERVER_ERROR, "/ error/500"))

Container.addErrorPages (new ErrorPage (HttpStatus.NOT_FOUND, "/ error/404"))

}

}

}

}

New Code:

@ Configuration

Public class ContainerConfig implements ErrorPageRegistrar {

@ Override

Public void registerErrorPages (ErrorPageRegistry registry) {

ErrorPage [] errorPages = new ErrorPage [2]

ErrorPages [0] = new ErrorPage (HttpStatus.INTERNAL_SERVER_ERROR, "/ error/500")

ErrorPages [1] = new ErrorPage (HttpStatus.NOT_FOUND, "/ error/404")

Registry.addErrorPages (errorPages)

}

}

After temporarily dealing with the above errors, the project is ready to start, and there are other hidden errors that will be supplemented later.

This is the end of the article on "how to upgrade Spring Boot1.5X to 2.0". Thank you for reading! I believe you all have a certain understanding of "how to upgrade Spring Boot1.5X to 2.0". If you want to learn more, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report