Spring Boot 配置 Jetty 容器_spring

一、添加 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 容器_后端_02

三、添加一个测试接口

添加一个测试接口:

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 容器_架构_03

大功告成,正常输出 success