这里我们将向您展示一个示例 - 在 Spring Boot 中使用 Undertow 作为嵌入式服务器
对于 Spring Boot 应用程序,您可以生成一个包含 Embedded Tomcat 或 Undertow 或 jetty 的应用程序 jar。您可以将 Web 应用程序作为普通 Java 应用程序运行!这样做的好处是您不需要在部署环境中预先安装服务器。

下面我们将展示一个使用 Undertow 作为嵌入式服务器的简单 Spring Boot 应用程序示例。

1. Maven/依赖管理

移除 spring-boot-starter-web 上已有的 tomcat 依赖,添加 undertow 依赖

 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
          https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>
    <groupId>com.knowledgefactory</groupId>
    <artifactId>springboot-embeddedserver-undertow</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-embeddedserver-undertow</name>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- spring mvc, rest -->
        <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>
        <!-- embedded undertow -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <url>www.knowledgefactory.net</url>
</project>

2. 测试控制器


 
package com.knowledgefactory.knowledgefactorydemo;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<String> listAllUsers() {
        return new ResponseEntity<String>
             ("Greetings from knowledgefactory",
                HttpStatus.OK);
    }
}

3. Spring Boot - 主类


 
package com.knowledgefactory.knowledgefactorydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KnowledgefactorydemoApplication {
    public static void main(String[] args) {
        SpringApplication.
           run(KnowledgefactorydemoApplication.class, args);
    }
}

4.运行

$ mvn spring-boot:run

5. 测试

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐