首页 > 解决方案 > Spring Boot:spring-starter-web 的用途

问题描述

我是Spring生态系统的新手,很多事情都很混乱。我有一个包含以下入门包的网络项目:spring-boot-starter-jerseyspring-boot-starter-jetty. 这将启动带有 Jersey servlet 的 Jetty 服务器。哪个好。但它不提供静态内容。所以,我安装了spring-boot-starter-web它,它为resources/static. 但我不明白怎么做。安装一个软件包只是为了提供静态内容似乎有点过头了。如果它是一个非 Spring 项目,我会使用 Jetty 创建一个新的 servlet 并链接一个base resource path. 但我不确定如何在不使用spring-starter-web.

那么,spring-starter-web在我的项目设置中是否有任何附加价值(不包括静态资源服务)?

如何在Jetty没有 ? 的情况下仅使用服务器静态资源starter-web

标签: javaspringspring-bootjettyembedded-jetty

解决方案


这个 Spring Boot Starter Web 使用 Spring MVC、REST 和 Tomcat 作为默认的嵌入式服务器,因此您需要对其进行调整以将其替换为 Jetty。您可以通过以下方式做到这一点:

<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>

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自动配置 Web 开发通常需要的以下功能:

  • 调度程序 Servlet
  • 错误页面
  • 用于管理静态依赖项的 Web JAR
  • 嵌入式 servlet 容器

说了这么多,除非您真的想对项目的依赖项进行更细粒度的控制,否则我建议您使用spring-boot-starter-web它,因为它可以节省大量样板代码。


推荐阅读