Home  >  Article  >  Java  >  How to solve the problems encountered when SpringBoot project is packaged and released to external tomcat

How to solve the problems encountered when SpringBoot project is packaged and released to external tomcat

WBOY
WBOYforward
2023-05-10 17:49:141074browse

Let’s talk about the situation when we encountered the problem:

I tried to use the spring boot framework to write a small web project for the first time, and it could start and run normally in Intellij IDEA. Use maven to run install, generate the war package, and publish it to the local tomcat. An exception occurs. The main exception information is .....LifeCycleException. After various searches, I found the answer.

Because spring boot has an embedded tomcat container, the project can be released by packaging it as a jar package. But how to package the spring boot project into a war package project that can be released to tomcat?

1. Since you need to package it into a war package project, you first need to modify the packaging type in the pom.xml file, and change the spring boot default jar to warForm;

2. Secondly, the tomcat server is embedded in the spring boot web project, so if we want to publish the war package to the tomcat project, we must exclude the dependencies of the tomcat package embedded in spring boot. Otherwise, if a conflict occurs, just open the comments in the code below.

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-web</artifactid>
  <!--
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
  -->
</dependency>

One thing I want to say is that if you still want to use spring boot embedded tomcat for debugging during local development, just add the following dependencies;

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-tomcat</artifactid>
  <scope>provided</scope>
</dependency>

3. Spring boot releases jar The entry point of the package web program is the class where the main function is located, which is annotated with @SpringBootApplication. However, if the war package is published to tomcat, you need to add a SpringBootServletInitializer subclass and override its configure method, or directly inherit the class where the main function is located from the SpringBootServletInitializer subclass and overwrite its configure method. The code example is as follows,

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
   
  @Override
  protected SpringApplicationBuilder configure(
      SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
  }
 
   
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

The above completes all the steps for packaging the war package of the spring boot project, and can be published to tomcat7 and above.

Finally and the most important point is that if your local tomcat version is inconsistent with the 8.0 version automatically integrated by springboot. There will also be problems, the solution is:

My local Tomcat version is 8.0.28, and the Tomcat on the server is 7.0.69. I deployed Tomcat 7.0.70 locally and reported the same error. I am more certain that the problem is related to the Tomcat version. After searching for information in many ways, I finally saw a foreigner on Stackoverflow who said that the default Servlet container of SpringBoot is based on Tomcat8

I did find the Tomcat related jar package in the prepared war package, and it is Tomcat8, take Tomcat8 The embed package must not be used under Tomcat7

How to solve the problems encountered when SpringBoot project is packaged and released to external tomcat

To support lower versions of Tomcat, you need to specify the Tomat version in maven, and the configuration is as follows:

<properties>
  <tomcat.version>7.0.69</tomcat.version>
</properties>

Then add it to the dependency (this is actually fine without adding it, the official document adds it)

<dependency>
  <groupid>org.apache.tomcat</groupid>
  <artifactid>tomcat-juli</artifactid>
  <version>${tomcat.version}</version>
</dependency>

After adding it, I tried it, and it turned out to be no problem. After looking at the lib directory in the war package, it has indeed become a Tomcat7 package.

How to solve the problems encountered when SpringBoot project is packaged and released to external tomcat

#But I am still a little confused. Wouldn’t it be better to change the Tomcat version if the package is packaged like this? Want to repackage? Since this limitation is caused by the Servlet container inside SpringBoot, can I not use it? After checking a lot of information, I found a solution!


<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-tomcat</artifactid>
  <scope>provided</scope>
</dependency>

The above is the detailed content of How to solve the problems encountered when SpringBoot project is packaged and released to external tomcat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete