springboot 配置静态资源映射

配置web页面资源的映射

实现在springboot工程中访问静态的html页面;前端工程化,使用npm run build 打包生成的静态文件(html+js+css)直接拖到springboot工程下可实现部署

编辑 application.yml

spring:
    resources: # 指定静态资源的路径
        static-locations: classpath:/static/

上面配置的classpath:/static/ 路径就是指这里


image.png

编辑test.html示例页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello world

</body>
</html>

重启工程,访问 http://localhost:8080/test/test.html

image.png

映射到磁盘的绝对路径

我在E:\test里放了张图片,想通过http来访问


image.png

编辑配置文件

  • imagePath为磁盘路径
  • httpImagePath 为访问的url的前缀
image:
  imagePath: file:E:/test/
  httpImagePath: /image/**

需要写一个配置类了,实现WebMvcConfigurer 接口,重写ImageWebAppConfig 方法

package com.springboot.study.demo1.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ImageWebAppConfig implements WebMvcConfigurer {

    @Value("${image.imagePath}")
    private String imagePath;

    @Value("${image.httpImagePath}")
    private String httpImagePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(httpImagePath).addResourceLocations(imagePath);
    }
}

重启工程,访问http://localhost:8080/test/image/z3f.jpg

image.png

成功

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容