Spring Boot Web是基于Spring Boot构建Web应用的基础。Spring Boot Web默认引入了Tomcat、Spring MVC等依赖,方便开发者构建Web应用。

使用

基于Spring Boot 构建Web应用时,只需添加spring-boot-starter-web依赖。该依赖已引入Web容器(默认是Tomcat)、Spring MVC。这里简单记录下spring-boot-starter-web的直接依赖。

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.4.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.4.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.4.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.3</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

可以看到,spring-boot-starter-web添加了Tomcat(未直接依赖)、Spring MVC等依赖。另外,spring-boot-starter-web默认引入spring-boot-starter,所以无需单独引入spring-boot-starter依赖。

总结

spring-boot-starter-web默认使用Tomcat作为Web容器。Tomcat已成为Web应用托管的主流实现方式。Spring Boot Web相关示例可以参考spring-boot-example源码。

参考

https://spring.io/projects/spring-boot Spring Boot
https://spring.io/guides/gs/rest-service/ Building a RESTful Web Service

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐