Spring boot deployed to tomcat server!!! Super detailed!!!

A springboot project, or a demo, is not easy to write. It can run on the project, but it can't be accessed on tomcat. Let's talk about why in the end!!!

First of all, the train of thought

  1. To change the startup class, springboot needs to change its main startup class when it is published to tomcat
  2. Change the package type of springboot to war type in pom.xml
  3. Because springboot has built-in tomcat, you have to block your own Tomcat when you package projects
  4. In the springboot configuration file and pom.xml, declare the access path in tomcat

Step 1: change the springboot startup class

Let's take a look at the original startup class

@SpringBootApplication
public class DrawingBedApplication {
    public static void main(String[] args){
        SpringApplication.run(DrawingBedApplication.class, args);

    }
}

Let's look at what we want to change

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

I'm adding a dependency

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

Step 2: originally, it was packaged as jar package, now we change it to war package

   <groupId>com.hfq</groupId>
    <artifactId>drawing_bed</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- Packaged into WAR -->
    <packaging>war</packaging>

Step 3: shield the tomcat of springboot

<!-- Exclude built in tomcat Container, export to war Packages allow external containers to run spring-boot project-->
        <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>

Step 4: change the access path of our project on tomcat

Here's the point. Our problem is that the project was successfully released in tomcat (we can see the log of our whole project in the log folder of tomcat folder), but we can't access it. The reason is that the path is wrong
Because spring boot package will add suffix to you by default, such as:

OK, let's solve it
Step 1: change the default package name given to you by springboot

    <build>
    <!--Here you write the project path you want to visit XXXX-->
        <finalName>drawing_bed</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Step 2: since the package names of your projects have been changed, do you have to change the names of the projects you visit by tomcat? So let's configure the project access path in tomcat in the configuration file. I use yal here

server:
  servlet:
    context-path: /drawing_bed

OK, start packing:

Packaged successfully:

We can see that the war package name of our project has been changed

Then, put the war package into webapp in your tomcat folder
Originally, our packing was put into the folder in such a way that we could not access it

Now we've changed the name, changed the path, put it in

OK, in the bin folder of our apache-tomcat-8.0.33 folder, the shutdown.bat file first closes tomcat, and then starts Tomcat in startup.bat. If you flash back, it may be JAVA_HOME is not configured, or it is Catalan_ Home is not configured


At this time, if tomcat can't find JAVA_HOME that can only come to hard, directly change its configuration file
How to change? Let's see
Analyze the startup.bat startup script: it calls catalina.bat, and catalina.bat calls setclasspath.bat
To start the change:
set JAVA_HOME=D:\JAVA\JDK
Here you can configure Tomcat Java manually_ HOME
Successful project deployment!

Tags: Tomcat Spring Boot

Posted by richierich on Fri, 11 Jun 2021 06:14:34 +0930