个性化阅读
专注于IT技术分析

Spring Boot 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还支持另外两个嵌入式服务器:

  • 码头服务器
  • 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或spring -boot-starter-undertow代替。

码头服务器

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-jetty</artifactId>
</dependency>

我们还可以使用application.properties文件来自定义Jetty服务器的行为。

Undertow服务器

Spring Boot提供了另一台称为Undertow的服务器。它也是像Jetty这样的嵌入式Web服务器。它用Java编写, 由JBoss管理和赞助。 Undertow服务器的主要优点是:

  • 支持HTTP / 2
  • HTTP升级支持
  • Websocket支持
  • 提供对Servlet 4.0的支持
  • 灵活
  • 可嵌入

切记:在应用程序中使用Undertow服务器时, 请确保从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服务器的行为。

spring-boot-starter-web vs.spring-boot-starter-tomcat

spring-boot-starter-web包含spring网络依赖关系, 其中包括spring-boot-starter-tomcat。 spring-boot-starter-web包含以下内容:

  • Spring启动启动器
  • 杰克逊
  • Spring芯
  • SpringMV
  • Spring启动启动器tomcat

而spring-boot-starter-tomcat包含与Tomcat服务器相关的所有内容。

  • 核心
  • el
  • 测井
  • 网络套接字

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>

下载专案

下载专案


赞(0)
未经允许不得转载:srcmini » Spring Boot Starter Web用法详解

评论 抢沙发

评论前必须登录!