news 2026/3/11 2:24:24

Spring boot 4.0.1 嵌入的 web服务器,以及如何更换别的 web 服务器

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring boot 4.0.1 嵌入的 web服务器,以及如何更换别的 web 服务器

Spring boot 默认嵌入的 web 服务器 —— Tomcat 。

Each Spring Boot web application includes an embedded web server. This feature leads to a number of how-to questions, including how to change the embedded server and how to configure the embedded server. This section answers those questions.

当然可以更换别的。

thespring-boot-starter-webincludes Tomcat by includingspring-boot-starter-tomcat

Use Another Web Server

Many Spring Boot starters include default embedded containers.

许多Spring Boot启动器包含默认的嵌入式容器。

  • For servlet stack applications, thespring-boot-starter-webincludes Tomcat by includingspring-boot-starter-tomcat, but you can usespring-boot-starter-jettyinstead.

  • For reactive stack applications, thespring-boot-starter-webfluxincludes Reactor Netty by includingspring-boot-starter-reactor-netty, but you can usespring-boot-starter-tomcatorspring-boot-starter-jettyinstead.

对于Servlet栈应用,spring-boot-starter-web通过包含spring-boot-starter-tomcat默认引入Tomcat,但您也可以改用spring-boot-starter-jetty

对于响应式栈应用,spring-boot-starter-webflux通过包含spring-boot-starter-reactor-netty默认引入Reactor Netty,但您也可以改用spring-boot-starter-tomcatspring-boot-starter-jetty

When switching to a different HTTP server, you need to swap the default dependencies for those that you need instead. To help with this process, Spring Boot provides a separate starter for each of the supported HTTP servers.

切换到不同的 HTTP 服务器时,您需要用所需的依赖项替换默认依赖项。为了简化此过程,Spring Boot 为每个受支持的 HTTP 服务器提供了单独的启动器。

The following example shows how to exclude Tomcat and include Jetty for Spring MVC:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc</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>

If you are creating a war file, you can use a similar approach, but you must indicate provided dependencies:

如果您正在创建一个war文件,可以采用类似的方法,但必须指明提供的依赖项。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webmvc</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> <scope>provided</scope> </dependency>

Disabling the Web Server

禁用Web服务器

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behavior configure the WebApplicationType in yourapplication.properties, as shown in the following example:

如果您的类路径包含启动Web服务器所需的组件,Spring Boot将自动启动它。要禁用此行为,请在您的application.properties中配置WebApplicationType,如下例所示:

# YAML

spring:
main:
web-application-type: "none"

Change the HTTP Port

In a standalone application, the mainHTTP port defaults to8080but can be set withserver.port(for example, inapplication.propertiesor as a System property). Thanks to relaxed binding of Environment values, you can also useSERVER_PORT(for example, as an OS environment variable).

To switch off the HTTP endpoints completely but still create a WebApplicationContext, useserver.port=-1(doing so is sometimes useful for testing).

For more details, see Customizing Embedded Servlet Containers in the ‘Spring Boot Features’ section, or the ServerProperties class.

更改HTTP端口
在独立应用程序中,主HTTP端口默认为8080,但可通过server.port设置(例如在application.properties中或作为系统属性)。得益于环境值的宽松绑定,您也可以使用SERVER_PORT(例如作为操作系统环境变量)。

要完全关闭HTTP端点但仍创建WebApplicationContext,可使用server.port=-1(这样做有时对测试很有用)。

更多详细信息,请参阅“Spring Boot特性”部分中的[自定义嵌入式Servlet容器],或查看[ServerProperties]类。

Customizing Embedded Servlet Containers

Common servlet container settings can be configured by using Spring Environment properties. Usually, you would define the properties in yourapplication.propertiesorapplication.yamlfile.

Common server settings include:

  • Network settings: Listen port for incoming HTTP requests (server.port), interface address to bind to (server.address), and so on.

  • Session settings: Whether the session is persistent (server.servlet.session.persistent), session timeout (server.servlet.session.timeout), location of session data (server.servlet.session.store-dir), and session-cookie configuration (server.servlet.session.cookie.*).

  • Error management: Location of the error page (spring.web.error.path) and so on.

  • SSL

  • HTTP compression

Spring Boot tries as much as possible to expose common settings, but this is not always possible. For those cases, dedicated namespaces offer server-specific customizations (seeserver.tomcat). For instance, access logs can be configured with specific features of the embedded servlet container.

自定义嵌入式Servlet容器
常见的Servlet容器设置可以通过Spring Environment属性进行配置。通常,您会在application.propertiesapplication.yaml文件中定义这些属性。

常见的服务器设置包括:

网络设置:监听HTTP请求的端口(server.port)、绑定的接口地址(server.address)等。

会话设置:会话是否持久化(server.servlet.session.persistent)、会话超时时间(server.servlet.session.timeout)、会话数据存储位置(server.servlet.session.store-dir)以及会话Cookie配置(server.servlet.session.cookie.*)。

错误管理:错误页面路径(spring.web.error.path)等。

SSL
HTTP压缩

Spring Boot尽可能多地暴露通用设置,但并非所有情况都适用。对于这些情况,专用命名空间提供了针对特定服务器的自定义配置(例如server.tomcat)。例如,可以通过嵌入式Servlet容器的特定功能来配置访问日志。

https://docs.spring.io/spring-boot/reference/web/servlet.html#web.servlet.embedded-container.customizing

Use a Random Unassigned HTTP Port

To scan for a free port (using OS natives to prevent clashes) useserver.port=0.

Discover the HTTP Port at Runtime

You can access the port the server is running on from log output or from the WebServerApplicationContext through its WebServer. The best way to get that and be sure it has been initialized is to add a @Bean of typeApplicationListener<WebServerInitializedEvent>and pull the container out of the event when it is published.

Tests that use@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)can also inject the actual port into a field by using the @LocalServerPort annotation, as shown in the following example:

运行时发现HTTP端口
您可以从日志输出或通过WebServerApplicationContextWebServer访问服务器运行的端口。确保端口已初始化的最佳方式是添加一个ApplicationListener<WebServerInitializedEvent>类型的@Bean,并在事件发布时从中提取容器。

使用@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)的测试还可以通过@LocalServerPort注解将实际端口注入字段,如下例所示:

import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.server.LocalServerPort; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class MyWebIntegrationTests { @LocalServerPort int port; // ... }

Enable HTTP Response Compression

启用HTTP响应压缩

HTTP response compression is supported by Jetty, Tomcat and Reactor Netty. It can be enabled inapplication.properties, as follows:

Jetty、Tomcat和Reactor Netty均支持HTTP响应压缩功能。可通过在application.properties文件中进行如下配置来启用:

# YAML

server:
compression:
enabled: true

By default, responses must be at least 2048 bytes in length for compression to be performed. You can configure this behavior by setting theserver.compression.min-response-sizeproperty.

默认情况下,只有当响应内容至少达到2048字节时才会执行压缩。您可以通过设置server.compression.min-response-size属性来配置此行为。

By default, responses are compressed only if their content type is one of the following:

  • text/html

  • text/xml

  • text/plain

  • text/css

  • text/javascript

  • application/javascript

  • application/json

  • application/xml

You can configure this behavior by setting theserver.compression.mime-typesproperty.

Configure SSL

SSL can be configured declaratively by setting the variousserver.ssl.*properties, typically inapplication.propertiesorapplication.yaml. See Ssl for details of all of the supported properties.

配置SSL
可以通过声明式地设置各种server.ssl.*属性来配置SSL,通常在application.propertiesapplication.yaml文件中进行。有关所有支持属性的详细信息,请参阅Ssl

The following example shows setting SSL properties using a Java KeyStore file:

server:
port: 8443
ssl:
key-store: "classpath:keystore.jks"
key-store-password: "secret"
key-password: "another-secret"

Using configuration such as the preceding example means the application no longer supports a plain HTTP connector at port 8080. Spring Boot does not support the configuration of both an HTTP connector and an HTTPS connector throughapplication.properties. If you want to have both, you need to configure one of them programmatically. We recommend usingapplication.propertiesto configure HTTPS, as the HTTP connector is the easier of the two to configure programmatically.

使用如上示例的配置意味着应用程序不再支持8080端口的普通HTTP连接器。Spring Boot不支持通过application.properties同时配置HTTP和HTTPS连接器。如需同时启用这两种连接器,需要通过编程方式配置其中一种。我们建议使用application.properties配置HTTPS,因为HTTP连接器是两者中更容易通过编程方式配置的选项。

Using PEM-encoded files

You can use PEM-encoded files instead of Java KeyStore files. You should use PKCS#8 key files wherever possible. PEM-encoded PKCS#8 key files start with a-----BEGIN PRIVATE KEY-----or-----BEGIN ENCRYPTED PRIVATE KEY-----header.

If you have files in other formats, e.g., PKCS#1 (-----BEGIN RSA PRIVATE KEY-----) or SEC 1 (-----BEGIN EC PRIVATE KEY-----), you can convert them to PKCS#8 using OpenSSL:

使用PEM编码文件 您可以使用PEM编码文件替代Java密钥库文件。应尽可能使用PKCS#8格式的密钥文件。PEM编码的PKCS#8密钥文件以-----BEGIN PRIVATE KEY-----或-----BEGIN ENCRYPTED PRIVATE KEY-----开头。

如果您持有其他格式的文件,例如PKCS#1(-----BEGIN RSA PRIVATE KEY-----)或SEC 1(-----BEGIN EC PRIVATE KEY-----),可以使用OpenSSL将其转换为PKCS#8格式:

openssl pkcs8 -topk8 -nocrypt -in <input file> -out <output file>

The following example shows setting SSL properties using PEM-encoded certificate and private key files:

以下示例展示了使用PEM编码的证书和私钥文件设置SSL属性:

server: port: 8443 ssl: certificate: "classpath:my-cert.crt" certificate-private-key: "classpath:my-cert.key" trust-certificate: "classpath:ca-cert.crt"

更多内容 https://docs.spring.io/spring-boot/how-to/webserver.html

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/5 10:42:34

小白必看:什么是WiFi密码字典及其基本用法

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个交互式WiFi密码字典学习应用&#xff0c;通过简单示例演示密码字典的工作原理。要求包含基础知识讲解、简单字典生成演示和实际应用场景说明。使用HTMLJavaScript实现可视化…

作者头像 李华
网站建设 2026/3/6 14:29:11

传统调试 vs AI辅助:解决Internal Server Error的效率对比

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 构建一个对比工具&#xff0c;左侧展示传统调试步骤&#xff08;查看日志、手动排查等&#xff09;&#xff0c;右侧展示AI辅助调试流程&#xff08;自动分析、建议修复&#xff09…

作者头像 李华
网站建设 2026/3/10 20:06:56

系统迁移时如何处理Temp文件夹?专家建议

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个系统迁移辅助工具&#xff0c;专门处理Temp目录&#xff1a;1) 分析临时文件使用情况 2) 智能识别需要保留的文件 3) 生成迁移报告 4) 支持自定义过滤规则 5) 与主流迁移工…

作者头像 李华
网站建设 2026/3/8 4:05:32

姬无烦科幻与张祥前统一场论的完美融合

姬无烦科幻与张祥前统一场论的完美融合 引言&#xff1a;科幻与科学的奇妙邂逅 当科幻作家的想象力与物理学家的公式相遇&#xff0c;会碰撞出怎样的火花&#xff1f; 在《外星文明与人类未来》这部姬无烦的科幻小说中&#xff0c;我们看到了一个充满奇迹的未来&#xff1a;飞碟…

作者头像 李华
网站建设 2026/3/10 4:26:21

Java并发编程面试题:ThreadLocal(8题)

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

作者头像 李华
网站建设 2026/3/11 11:31:10

消息队列设计:从同步到异步的性能突破

前言 2024年初&#xff0c;我们的订单系统经常出现"超时"问题。用户下单后&#xff0c;系统需要同时调用库存服务、支付服务、通知服务&#xff0c;任何一个服务慢都会导致整个请求超时。 我们决定引入消息队列&#xff0c;将同步调用改为异步处理。这个改造带来了…

作者头像 李华