This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

No, because SpringBoot already has three built-in containers: Tomcat, Jetty, and Undertow.

When we add the spring-boot-starter-Web dependency, Tomcat is used as the web container by default.

Each Spring Boot Web application has a built-in server. This feature leads to a series of “how to” questions. For example, how to change the built-in server, how to configure the built-in server, etc., these questions are answered below.

The configuration file

Configuration changes

There are many ways to change the configuration, such as:

  • Register a Spring bean, implementationWebServerFactoryCustomizerInterface.WebServerFactoryCustomizerProvides forConfigurableServletWebServerFactory, which includes a number of custom Settings, such as the following example demonstrates setting ports programmatically:
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); }}Copy the code
  • Register directlyTomcatServletWebServerFactory.
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
	TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
	factory.setPort(9000);
	factory.setSessionTimeout(10, TimeUnit.MINUTES);
	factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
	return factory;
}
Copy the code
  • inapplication.ymlFile configuration.

The usual way to define the configuration is through application.yml. The following example shows how to change the server configuration.

The default configuration

About the Tomcat’s default configuration is in org. Springframework. The boot: spring – the boot – autoconfigure: 2.3.0. The jar jar package defined.

Let’s take the most common port parameter as an example to see how the default configuration in Spring Boot is defined:

    {
      "name": "server.port",
      "type": "java.lang.Integer",
      "description": "Server HTTP port.",
      "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
      "defaultValue": 8080
    },
Copy the code

From the above configuration, we can see the complete definition of server.port. If we do not specify a port number, the default port number is 8080, as are the other parameters.

The same is true for other configurations

Commonly used configuration

There are many configurations for Tomcat, but in general, we only focus on some parameter information. Here are some common configurations.

Server: port: 8081 # default 8080 servlet: context-path: /search # set the project name (default: /). 127.0.0.1 # server binding address error: path: /error # configure the current project error to jump to the page. Tomcat: connection-timeout: 5s # Server connection timeout Max -swallow-size: 2MB # Request body size uri-encoding: encoding Basedir: /home/work/search Configur es the directory for Tomcat run logs and temporary files. If this parameter is not specified, the system temporary directory is used by default. Max-http-header-size: 8KB # Maximum size of the request header SSL: enabled: true # Enable SSL Supported protocol: TLS # Protocol used by SSLCopy the code

You can modify other configurations if necessary.

Replacing the built-in server

When we want to replace our built-in servers, we first need to remove the default dependencies and then introduce the dependencies we need.

An example of how to replace Tomcat (Maven) with Jetty.

< the properties > < servlet - API. Version > 3.1.0 < / servlet - API. Version > < / properties > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <! -- Exclude the Tomcat dependency --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <! -- Use Jetty instead --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>Copy the code

Reference documentation

  • SpringBoot – Internal Tomcat server configuration details
  • Appendix A. Common application properties
  • 77. Embedded Web Servers
  • 28.4.4 Customizing Embedded Servlet Containers
  • How do I configure Spring Boot Tomcat