Spring boot project releases tomcat container

Because spring boot has a built-in 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 it needs to be packaged into a war package project, you first need to modify the packaging type in the pom.xml file, and change the default <packaging>jar</packaging> of spring boot to the form of <packaging>war</packaging>;

2. Second The tomcat server is embedded in the web project of spring boot, so if we want to publish the war package to the tomcat project, we need to exclude the dependency of the tomcat package embedded in spring boot, otherwise there will be conflicts, just open the comments in the following code.

<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, you can add the following dependencies:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>


3. The entry of the spring boot jar package web program is the class where the main function is located, which is annotated with @SpringBootApplication. However, if the war package is released to tomcat, you need to add a SpringBootServletInitializer subclass and override its configure method, or directly inherit the SpringBootServletInitializer subclass from the class where the main function is located and override 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 of packaging the war package of the spring boot project, which can be released to tomcat7 and above.

However, after the above process is modified and the spring boot packaged war package is released to the tomcat6 version, the browser will give a 404 error when accessing the project address? Why, I am at a loss. After some research and experiments, I came to the following conclusions.

First of all, the servlet containers supported by spring boot are as follows. It can be seen that the minimum servlet version supported by spring boot is 3.0, but the servlet version of tomcat6 is 2.5 , in this case, the above process cannot support the tomcat6 release of the spring boot project,



but I googled it again and found that someone has already solved this problem, https://github.com/dsyer/spring-boot-legacy, the


title is It shows that it is to allow spring boot to support servlet 2.5, which just solves our pain point. The steps are as follows:

1. Add spring-boot-legacy dependency to pom.xml,
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-legacy</artifactId>
    <version>1.1.0.RELEASE</version>
</dependency>


2. Manually replace the web.xml file. However, in the release war package, I found that the metricFilter prompts a null pointer exception, so I simply and rudely filtered the filter, and the comments are as follows. The unknown web.xml file to be replaced is as follows: {project directory}/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.example.DemoApplication</param-value>
    </context-param>
 
    <listener>
        <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
    </listener>
 
<!--
    <filter>
        <filter-name>metricFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>metricFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
-->
 
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>


After completing the above two steps, the spring boot project can support the deployment of tomcat6, which is solved.

Thinking: Spring boot encapsulation brings convenience, but also brings the complexity of solving problems. If you do not understand the original spring web development model, it is difficult to solve problems. Even if I solve the spring boot support problem that supports tomcat6 now, I don't quite understand the principle of the solution. Why does the filter have a null pointer? Therefore, it is very important to go deep into some fundamental things and learn the basic things of technology. Everyone can understand and explain the principle of 2.5 support and the reason for the exception of the filter null pointer.

http://www.cnblogs.com/weixliu/p/6432342.htmlDeploy

the spring-boot project to the tomcat containerhttps:
//www.cnblogs.com/a8457013/p/7687820.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326383753&siteId=291194637