Spring5 全家桶 | 35 - Spring Boot 2.x WebMvcAutoConfiguration

2,035 阅读4分钟

“Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情。”

一、Spring Boot 中的 Web 开发

使用IDEA创建项目Spring Boot项目spring-boot-restful,选择基本的Web依赖和Thymeleaf模板引擎依赖。

image.png Spring Boot已经的自动配置已经完成了大量的配置,我们只需要少量的配置就可以完成一个Web工程的创建。

在com.lilith包下新建controller包,增加HelloController,增加hello方法以及使用注解配置访问路径。

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello, Spring Boot!";
    }
}

启动主程序,在浏览器输入 http://localhost:8080/hello image.png

通过这几步,一个Web工程就创建完成了,相比Spring MVC,几乎没有做任何配置,配置全部由Spring Boot中大量的XxxAutoConfiguration自动配置类完成,可以自定义的配置全部在XxxProperties配置类中。

Spring Boot对静态资源的映射规则

公共静态资源访问方式

Spring Boot 的 Web自动配置类是 org.springframework.boot.autoconfigure.web.servlet .WebMvcAutoConfiguration;在该类中的addResourceHandlers 方法是用于定义资源访问的,这个方法中添加了webjars的访问路径;也就是说所有的/webjars/** ,都可以去类路径下既classpath:/META-INF/resources/webjars/查找资源

image.png webjars就是将前端资源以jar包的方式进行访问;前端资源的jar包可以在 webjars官网 获取。

在pom文件中添加jQuery的jar包

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

引入的jquery静态资源就在webjars目录下 image.png

重新启动应用,在浏览器访问静态资源 http://localhost:8080/webjars/jquery/3.3.1/jquery.js image.png

私有静态资源访问方式

对于私有静态静态资源的访问方式定义在addResourceHandlers方法中的第338行

image.png

this.mvcProperties.getStaticPathPattern()

上面这行代码代表的路径是 /** image.png 接着的lambda表达式代码代表的路径如下图所示 image.png

addResourceHandlers方法中定义的第二种访问方式是访问当前项目的任何资源既/**,如果没有方法处理,就自动去以下这些目录位置去查找资源

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/ 这些文件夹也称为静态资源文件夹。

尝试访问这几个路径,分别新建对应的文件夹,resources目录就是classpath:/。

classpath:/META-INF/resources/

新建META-INF/resources文件夹,添加一个index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/META-INF/resources目录</h1>
</body>
</html>

重新启动应用,在浏览器中访问 localhost:8080/index.html image.png 成功访问到META-INF/resources 目录下的index.html文件

classpath:/resources/

在原始的resources目录下再新建一个resources目录,注意不是META-INF下的resources目录,放入index1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/resources目录</h1>
</body>
</html>

重新启动应用,浏览器访问localhost:8080/index1.html image.png

classpath:/static/

在classpath:/ 类路径下的static文件夹新建index2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/static目录</h1>
</body>
</html>

重新启动应用,浏览器访问localhost:8080/index2.html image.png

classpath:/public/

在classpath:/下新建public文件夹,添加index3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h1>classpath:/public/</h1>
</body>
</html>

重新启动应用,浏览器访问localhost:8080/index3.html

image.png 也可以成功访问

Spring Boot 欢迎页

Spring Boot 中的WelcomePageHandlerMapping类中定义了欢迎页的配置

image.png 也就是说 / 路径会转发到 静态资源文件夹下的index.html页面上

在浏览器输入localhos:8080 image.png 根据页面显示默认找到了在META-INF/resources目录下的index.html文件作为欢迎页

自定义静态资源路径

WebProperties下的Resources类属性中有一个setStaticLocations方法,该方法可以自定义静态文件夹的路径 image.png

在properties配置文件中配置自定义的静态资源路径

# 覆盖以前所有的静态资源路径
spring.web.resources.static-locations=classpath:/lilith,

启动程序,再次访问index1.html

image.png

找不到index1.html页面,说明默认的静态文件夹已经不再是静态文件夹了,被自定义的设置覆盖了。需要注意的是欢迎页可以正常访问

在classpath:/路径下新增lilith文件夹,新增index3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/lilith</h1>
    <h2>这是自定义的静态资源配置</h2>
</body>
</html>

重启启动程序,访问index3.html

image.png 自定义的静态资源文件夹生效,可以正常访问。

ICON 配置

在Spring Boot项目的issues中提出,如果提供默认的Favicon可能会导致网站信息泄露。如果用户不进行自定义的Favicon的设置,而Spring Boot项目会提供默认的上图图标,那么势必会导致泄露网站的开发框架。

因此,在Spring Boot2.2.x中,将默认的favicon.ico移除,同时也不再提供上述application.properties中的属性配置。

点击查看Spring Boot icon issue