在 Spring Boot 中配置嵌入式 Undertow 服务器

添加 Undertow 依赖项

我们需要在这里做两件事:-

  1. spring-boot-starter-tomcat排除添加的默认依赖项spring-boot-start-web
  2. 添加spring-boot-starter-undertow依赖。
pom.xml
<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
build.gradle
dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module:'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
}

而已。您已将 tomcat 替换为 Undertow 服务器。

应用程序启动日志

当您启动 Spring Boot 应用程序时,您将在日志中显示 Undertow 现在正在为您的 Web 应用程序提供服务:-

INFO c.e.demo.SpringBootDemoApplication       :  Starting SpringBootDemoApplication using Java 11.0.10 on Ashishs-MBP with PID 5166 (/Users/ashl/IdeaProjects/springboot-examples/springboot-config/build/classes/java/main started by ashl in /Users/ashl/IdeaProjects/springboot-examples/springboot-config)
DEBUG c.e.demo.SpringBootDemoApplication      :  Running with Spring Boot v2.5.0, Spring v5.3.7
INFO c.e.demo.SpringBootDemoApplication       :  No active profile set, falling back to default profiles: default
WARN io.undertow.websockets.jsr               :  UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
INFO io.undertow.servlet                      :  Initializing Spring embedded WebApplicationContext
INFO w.s.c.ServletWebServerApplicationContext :  Root WebApplicationContext: initialization completed in 753 ms
INFO io.undertow                              :  starting server: Undertow - 2.2.7.Final
INFO org.xnio                                 :  XNIO version 3.8.0.Final
INFO org.xnio.nio                             :  XNIO NIO Implementation Version 3.8.0.Final
INFO org.jboss.threads                        :  JBoss Threads version 3.1.0.Final
INFO o.s.b.w.e.undertow.UndertowWebServer     :  Undertow started on port(s) 8080 (http)
INFO c.e.demo.SpringBootDemoApplication       :  Started SpringBootDemoApplication in 1.858 seconds (JVM running for 2.21)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容