Spring Boot配置嵌入Tomcat

x33g5p2x  于2022-09-30 转载在 Spring  
字(1.6k)|赞(0)|评价(0)|浏览(329)

在本教程中,你将学习如何配置嵌入SpringBoot中的默认**Web服务器。

默认情况下,Spring Boot自动配置默认的Tomcat服务器,用于默认Web根上下文("/")的所有请求。配置嵌入式Tomcat的最简单方法是通过属性。

下面是一个带有一些常见属性的application.properties配置实例。

# Server HTTP port server.port=8080   # Path of the error controller. server.error.path=/error   # Enable the default error page displayed in browsers in case of a server error. server.error.whitelabel.enabled=true   # Value to use for the Server response header (no header is sent if empty) server.server-header=   # Path of the main dispatcher servlet. server.servlet-path=/   # If response compression is enabled. server.compression.enabled=false   # Context path of the application. server.context-path=/myapp  # Display name of the application.  server.display-name=application   # When to include a "stacktrace" attribute. server.error.include-stacktrace=never

属性的完整列表包含在类org.springframework.boot.autoconfigure.web.ServerProperties

###以编程方式发现网络服务器的端口

你可以从日志输出或从ServletWebServerApplicationContext通过其WebServer发现服务器运行的端口。下面是一个例子。

public class MyWebIntegrationTests {
  @Autowired ServletWebServerApplicationContext server;
  @LocalServerPort int port;
}

如何禁用嵌入式Web服务器

如果你的classpath包含启动Web服务器的必要位,Spring Boot会自动启动它。要禁用这种行为,请在你的application.properties中配置WebApplicationType,如以下例子所示。

spring.main.web-application-type=none

在Web服务器中配置安全性

SSL可以通过设置各种server.ssl.*属性来声明性地配置,通常在application.propertiesapplication.yml中。下面的例子显示了在application.properties中设置SSL属性。

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

如果你想切换到Undertow Web服务器,请查看这个教程。配置Spring Boot以使用Undertow Web服务器

如果你想切换到Jetty Web服务器,请查看此教程。配置Spring Boot以使用Jetty服务器

相关文章

微信公众号

最新文章

更多