spring-boot使用教程(三):tomcat切换jetty

前言

spring-boot默认使用Tomcat作为内嵌的servlet容器,但是我们可以选择使用其它的容器比如:jetty、Undertow,下面我们试试将tomcat换成jetty

参考项目:https://github.com/bigbeef/cppba-sample
开源地址:https://github.com/bigbeef
个人博客:http://blog.cppba.com

搭建一个简单的spring-boot项目

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cppba-jetty</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

<parent>
<groupId>com.cppba</groupId>
<artifactId>cppba-sample</artifactId>
<version>1.0.0</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>

JettyApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.cppba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

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

运行项目

我们可以看到tomcat的提示语句

切换jetty

修改pom依赖

其实很简单,我们只需要修改一下pom的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
<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-jetty</artifactId>
</dependency>
</dependencies>

运行项目

这时控制台打印的时jetty的提示语句