SpringBoot Starter Web
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的启动程序使用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开发所需的以下各项:
调度程序Servlet
错误页面
用于管理静态依赖项的Web JAR
嵌入式servlet容器
Spring Boot嵌入式Web服务器
每个Spring Boot应用程序都包含一个嵌入式服务器。嵌入式服务器被嵌入为可部署应用程序的一部分。嵌入式服务器的优点是,我们不需要在环境中预安装服务器。使用Spring Boot,默认的嵌入式服务器为
Tomcat 。 Spring Boot还支持另外两个嵌入式服务器:
Jetty服务器
Undertow服务器
使用其他嵌入式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,
Jetty服务器
Spring Boot还支持称为
Jetty服务器。它是一个HTTP服务器和Servlet容器,具有提供静态和动态内容的功能。
如果要在应用程序中添加Jetty服务器,则需要添加
spring-boot-starter-jetty 依赖项。
记住: 。在应用程序中使用Jetty服务器时,请确保
排除了默认的Tomcat服务器
spring-boot-starter-web 。它避免了服务器之间的冲突。
<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>
我们还可以使用
application.properties 文件来自定义Jetty服务器的行为。
Undertow Server
Spring Boot提供了另一个名为
Undertow 的服务器。它也是像Jetty这样的嵌入式Web服务器。它用Java编写,由JBoss管理和赞助。 Undertow服务器的主要优点是:
支持HTTP/2
HTTP升级支持
Websocket支持
提供对Servlet 4.0的支持
灵活
可嵌入
记住: : 在应用程序中使用Undertow服务器时,请确保从
spring-boot-starter中排除了默认的Tomcat服务器 -web。避免了服务器之间的冲突。
<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服务器的行为。
spring-boot-starter-web vs. spring-boot-starter-tomcat
spring-boot-starter-web包含spring web依赖项,其中包括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>
我们也可以使用
spring-mvc 而不使用嵌入式Tomcat服务器。如果要这样做,我们需要使用
标记排除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>