繁体   English   中英

Spring 启动:Static 使用可执行文件时找不到资源 jar

[英]Spring boot: Static resource not found when using an executable jar

我的情况:资源目录:

--resources
  -- static
    -- css
      -- a.css
      -- b.css
    -- js
      -- aa.js
      -- bb.js

  -- templates
    -- index.html

index.html的一些代码:

<link href="css/a.css" rel="prefetch">
<link href="css/b.css" rel="prefetch">
<link href="js/aa.js" rel="prefetch">
<link href="js/bb.js" rel="prefetch">

在 Eclipse IDE 当我启动应用程序时,index.html 将被正确呈现并且可以获取 js/css。 当我在jar中打包并启动'java -jar xxx.jar'时,找不到js/css。

怎么会?

我尝试了一些解决方案但没有用:

一种。 我在 application.yml 中添加以下配置

spring:
  resources:
    static-locations: classpath:/static/

不工作

b. 添加此配置

@Configuration
public class WebConfig implements WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

}

不工作

c.在pom.xml中添加

<resources>
            <resource>
                <directory>src/main/resources/static/</directory>
                <filtering>true</filtering>
                <targetPath>META-INF/resources</targetPath>
            </resource>
</resources>

不起作用。

我检查 static 目录已经打包到 jar 和 jar 目录是这样的:

-- xxx.jar
  -- BOOT-INF
    -- static
      -- css
        -- a.css
        -- b.css
      -- js
        -- aa.js
        -- bb.js

任何人都知道如何解决它?

也许路径是错误的。

而不是 href="css/a.css" 使用 href="/css/a.css"

/ 在路径的开头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM