Spring Boot 配置 Jetty 容器

更新时间: 2021-03-21 17:29:47   作者: 异常教程网

本节中,您将学习如何在 Spring Boot 中配置 Jetty 容器。首先,让我们新建一个 Spring Boot Web 项目

一、添加 Maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除掉默认支持的 Tomcat -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 添加 jetty 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

二、启动项目,看下效果

观察控制台输出日志,当有 Jetty started on port(s) 8080 (http/1.1) with context path '/' 语句输出时,表明此时使用已是 Jetty 容器,而并非是默认的 Tomcat 容器了:

Spring Boot Jetty 日志输出

三、添加一个测试接口

添加一个测试接口:

package site.exception.springbootjetty.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author www.exception.site (exception 教程网)
 * @date 2019/2/25
 * @time 19:58
 * @discription
 **/
@RestController
public class TestController {

    @GetMapping("/test")
    public String test() {
        return "success !";
    }
}

重启项目,访问 http://localhost:8080/test, 查看效果:

Spring Boot Jetty 访问接口

大功告成,正常输出 success

三、GitHub 源码

https://github.com/weiwosuoai/spring-boot-tutorial/tree/master/spring-boot-jetty