spring-boot-starter-web
有两个重要功能:
- 与Web开发兼容
- 自动配置
如果要开发Web应用程序,则需要在pom.xml文件中添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
Spring Web Starter使用Spring MVC,REST和Tomcat作为默认的嵌入式服务器。 单个spring-boot-starter-web
依赖关系可传递地获取与Web开发相关的所有依赖关系。 它还减少了构建依赖项计数。 spring-boot-starter-web
可传递地取决于以下内容:
org.springframework.boot:spring-boot-starter
org.springframework.boot:spring-boot-starter-tomcat
org.springframework.boot:spring-boot-starter-validation
com.fasterxml.jackson.core:jackson-databind
org.springframework:spring-web
org.springframework:spring-webmvc
默认情况下,spring-boot-starter-web
包含以下tomcat服务器依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
spring-boot-starter-web
自动配置Web开发所需的以下各项:
Dispatcher Servlet
- 错误页面
- 用于管理静态依赖项的Web JAR
- 嵌入式Servlet容器
1.Spring Boot嵌入式Web服务器
每个Spring Boot应用程序都包含一个嵌入式服务器。 嵌入式服务器被嵌入为可部署应用程序的一部分。 嵌入式服务器的优点是,我们不需要在环境中预安装服务器。 使用Spring Boot,默认的嵌入式服务器是Tomcat。 Spring Boot还支持另外两个嵌入式服务器:
- Jetty Server
- Undertow Server
使用另一个嵌入式Web服务器
对于servlet堆栈应用程序,spring-boot-starter-web
通过包含spring-boot-starter-tomcat
来包括Tomcat,但是可以改用spring-boot-starter-jetty
或spring-boot-starter-undertow
。
对于反应堆应用程序,spring-boot-starter-webflux
通过包含spring-boot-starter-reactor-netty
来包含Reactor Netty,但是我们可以使用spring-boot-starter-tomcat
,spring-boot-starter-jetty
或spring -boot-starter-undertow
代替。
Jetty服务器
Spring Boot还支持称为Jetty Server的嵌入式服务器。 它是一个HTTP服务器和Servlet容器,具有提供静态和动态内容的功能。 需要机器间通信时使用。
如果要在应用程序中添加Jetty服务器,则需要在pom.xml
文件中添加spring-boot-starter-jetty
依赖项。
切记:在应用程序中使用Jetty服务器时,请确保从spring-boot-starter-web
中排除了默认的Tomcat服务器。 它避免了服务器之间的冲突。
<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>
还可以使用application.properties
文件来自定义Undertow服务器的行为。
2.spring-boot-starter-web与spring-boot-starter-tomcat
spring-boot-starter-web
包含spring网络依赖关系,其中包括spring-boot-starter-tomcat
。 spring-boot-starter-web
包含以下内容:
spring-boot-starter
jackson
spring-core
spring-mvc
spring-boot-starter-tomcat
而spring-boot-starter-tomcat
包含与Tomcat服务器相关的所有内容。它们如下:
core
el
logging
websocket
starter-tomcat
具有以下依赖性:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
也可以不使用嵌入式Tomcat服务器而使用spring-mvc
。 如果要这样做,需要使用<exclusion>
标记排除Tomcat服务器,如以下代码所示:
<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>