Although the general microservices are the Spring Boot project into jar directly launched, but war package as a classic project startup mode, it is necessary to master.

The complete project code has been uploaded to github: github.com/neatlife/my…

Writing takes a lot of the author’s time, from thumbs up to attention _(:з “Angle)_

Prepare case projects

You can modify the existing Spring Boot project directly, or you can create a new case project at https://start.spring.io/, such as:

If you are creating a new project on start.spring. IO and need to add a Web component to the project, the write test interface will use the controller in the Web component.

Then open the project in IDEA and write two apis for testing

@RestController
@RequestMapping("test")
public class MyController {
    @RequestMapping("test1")
    public String test1(a) {
        return "test1";
    }

    @RequestMapping("test2")
    public String test2(a) {
        return "test2"; }}Copy the code

Package the project into a WAR package

XML to specify a war package: WAR
Modify pom. XML to specify a scope to provide the Tomcat dependencies built into Spring Boot

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>
Copy the code

The modification results are as follows:

To create an initialization file initialization project such as: MySpringBootServletInitializer. Java core code is as follows

public class MySpringBootServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        returnbuilder.sources(MywarApplication.class); }}Copy the code

Run the MVN clean install-dmaven.test. skip=true command to create a war package and go to the target directory to check the effect

Run the project war in Tomcat

You can use Docker to start Tomcat with one click and mount the target directory where the project WAR is located into tomcat’s webapps directory

docker run --name=tomcat -itd --rm -p 8888:8080 \
  -v /Users/suxiaolin/Dropbox/Projects/mywar/target:/usr/local/tomcat/webapps tomcat:8-jre8
Copy the code

You can view tomcat startup logs using Docker logs -f tomcat

Open the browser to view the effect

Note that the url prefix is the full package name of the WAR package. The full URL is shown below

http://127.0.0.1:8888/mywar-0.0.1-SNAPSHOT/test/test1
http://127.0.0.1:8888/mywar-0.0.1-SNAPSHOT/test/test2
Copy the code

A couple of points to note

You can change the name of the final WAR package, such as removing the version number from the file name: mywar.war, using the finalName directive in POM.xml

Repackage to see the effect

This makes it easy to access the URL

Unsupported Major. Minor version 52.0 Unsupported major, minor version 52.0 (unable to load the class org. Springframework. Web. SpringServletContainerInitializer)

This is because the Java version packaged by the project is inconsistent with that of Tomcat. Currently, Java 8 is generally used, and tomcat docker image is tomcat:8-jre8