spring boot前端代码打包在一起资源访问配置

1、前端代码放置位置

在这里插入图片描述

2、配置输入端口后直接跳转到文件访问

@Controller
public class ViewController {

    @RequestMapping(value = {"/"})
    public String redir(){
        return "redirect:/index.html";
    }
}

3、配置访问


@Configuration
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String dirPath = AppProperties.getProperty("server.resource");
        String cachePath = AppProperties.getProperty("server.resource") + AppConstant.FileCacheDirName;
        checkDir(dirPath);
        checkDir(cachePath);
        checkDir(dirPath + "paper");
        checkDir(dirPath + "pics");
        checkDir(dirPath + "otherFile");
        registry.addResourceHandler(AppConstant.ResourceDir + "/**")
                .addResourceLocations("file:" + dirPath);

        registry.addResourceHandler(cachePath + "/**")
                .addResourceLocations("file:" + cachePath);

        //其他文件配置在static、资源保存根目录下查找,注意两个都要配置,否则只会在一个地方下查找文件
        registry.addResourceHandler("/**")
                .addResourceLocations("file:" + dirPath)
                .addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

    }


    private void checkDir(String dirFile) {
        File htmlFile = new File(dirFile);
        if (!htmlFile.exists()) {
            htmlFile.mkdirs();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值