Publish the spring boot project to the Tomcat container (including the methods published to Tomcat6)
Because spring boot is embedded with Tomcat container, the project can be published by packaging it as a jar package, but how to package the spring boot project into a war package project that can be published to Tomcat?
1. Since the project needs to be packaged as a war package, it needs to be in POM Modify the packaging type in the XML file, and change the default < packaging > jar < / packaging > of spring boot to < packaging > war < / packaging >;
2. Secondly, 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 should exclude the dependency of the Tomcat package embedded in spring boot, otherwise there will be a conflict. Open the comments in the following code.
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;
3. Spring boot publishes the jar package. The entry of the web program is the class where the main function is located. Use @ springbootapplication annotation. However, if the war package is published to tomcat, you need to add the 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. Code examples are as follows,
The above completes all steps of packaging war package for spring boot project, and can be released to tomcat7 and above.
However, after the above process is modified and the spring boot packaged war package is released to Tomcat6, the browser will give 404 error when accessing the project address? Why? I'm confused. After consulting the data and experiments, I come to the following conclusions,
Firstly, the servlet containers supported by spring boot are as follows. It can be seen that the minimum supported servlet version of spring boot is 3.0, but the servlet version of Tomcat6 is 2.5. In this case, the above process cannot support Tomcat6 to publish spring boot projects,
But Google did it again and found that someone was already solving the problem, https://github.com/dsyer/spring-boot-legacy ,
The title indicates that it is to make spring boot support servlet2 5. Just solve our pain points. The steps are as follows:
1. pom. Add spring boot legacy dependency to XML,
2. Manually replace web XML file. However, when I found the null pointer exception prompted by metricfilter in the released war package, I simply and rudely filtered the filter. The comments are as follows. The web to replace The unknown of the XML file is as follows: {project directory} / SRC / main / webapp / WEB-INF / Web xml
After completing the above two steps, you can enable the spring boot project to support the deployment of Tomcat 6. Solve the problem.
Thinking: spring boot encapsulation brings convenience and complexity to solve problems. If you don't understand the original spring web development mode, it is difficult to solve problems. Even though I have solved the spring boot support problem supporting Tomcat6, I don't quite understand the principle of the solution. Why does the null pointer appear in the filter? Therefore, it is very important to go deep into some principled things and learn the basic things of technology. You can clearly explain the principles supported by 2.5 and the reasons for the exception of filter null pointer.
PS: how to package and deploy spring boot projects to external Tomcat
1. Project packaging
After the project is developed, it needs to be packaged and deployed to Tomcat of external server. There are several ways.
(1) Generate jar package
After packaging, the jar package will be generated to the target directory, and the name is generally project name + version number jar
Start jar package command
In this way, as long as the console is closed, the service cannot be accessed. Let's start it by running in the background:
You can also choose to read different configuration files at startup
It can also be generated through Maven tool of idea. Click lifecycle install to generate jars
(2) Generate war package
(1) modify the packaging type to war: < packaging > war < / packaging >
(2) Add the spring boot starter Tomcat dependency, and set the scope to provided
(3) registered startup class
Create servletinitializer Java, inherit springbootservletinitializer, override configure (), and register the startup class application. When the external web application server builds the web application context, it will add the startup class.
It is also implemented in two ways:
(1)mvn clean package -Dmaven.test.skip=true
(2) generate war through Maven tool of idea and click lifecycle package
2. Project deployment
No matter which way it is generated, it is ultimately for deployment. War can be directly copied to the webapps directory of Tomcat and start Tomcat.
summary
The above is what Xiaobian introduced to you about publishing the spring boot project to Tomcat container (including the method of publishing to Tomcat6). I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!