spring boot starter web Method for replacing default Tomcat container

  • 2021-07-24 10:54:52
  • OfStack

Spring Boot supports automatic configuration of containers, and the default is Tomcat, which can be modified.

We know that Spring Boot supports automatic configuration of containers, and the default is Tomcat. Of course, we can also modify it:

1. First we exclude Tomcat from spring-boot-starter-web dependencies: Exclude starter from tomcat in pom file


<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>

2. Add Jetty container


<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

In this way, our springboot container is modified to Jetty container.

In order to facilitate our debugging, here we recommend a debugging tool for http: Postman

Let's talk about the global configuration file of springboot: application. properties

In the development, I must have encountered such a requirement, that is, to modify our container access port. Since springboot loads the container by default, the port setting is of course controlled by the configuration file, which is quite convenient. We only need to add it in the configuration file:

server.port=6666

In this way, our container port is changed to 6666.

We can also set the project access alias through the configuration file:

server.context-path=/springboot

So that our project can be accessed through http://localhost: 6666/springboot1

The above is just the tip of the iceberg of springboot configuration file configuration. For example, we can also set database connection configuration (database), development environment configuration and deployment environment configuration to realize seamless switching between them.

If you have any questions, please leave a message to this site, and this site will reply to you in time!


Related articles: