【问题标题】:Serving index page with Spring boot使用 Spring Boot 服务索引页面
【发布时间】:2017-11-15 11:25:31
【问题描述】:

我已经查看了所有内容,但我无法弄清楚我在打包的 spring-boot jar 文件中尝试为我的index.html 页面提供服务的方式做错了什么。这是我的应用结构:

当我运行 mvn clean install 时,它会正确打包 jar 并将 index.html 文件放入 jar 中的静态文件夹中。

这是我的 MVC 会议:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    registry.addResourceHandler("/app/**").addResourceLocations("/app/");
    registry.addResourceHandler("/**").addResourceLocations("/");
}

我做错了什么?

【问题讨论】:

  • 静态文件夹资源处理程序由 Sp​​ring Boot 开箱即用地提供。尝试删除您提供的自定义并再次运行该应用程序。您的页面应该可以从 localhost:8080/index.html 访问如果您仍然遇到问题,请分享更多详细信息,例如 HTTP 响应和任何 Java 错误。
  • 不幸的是,我没有收到任何 Java 错误或 HTTP 错误(除了 404)。提供页面是否需要任何其他配置?我摆脱了我的自定义资源处理程序,但不幸的是没有做任何事情:/

标签: java spring maven spring-mvc spring-boot


【解决方案1】:

好的,多亏了多方的帮助,我修复了它!这是我所做的,因此可能对其他人有所帮助:

1) 在 POM 中添加此资源。我仍然不确定为什么我需要这个,但没有这个我的应用程序将无法运行。 Spring boot 应该自动执行此操作,但显然不是。

<resources>
    <resource>
        <directory>src/main/resources/static</directory>
        <targetPath>static</targetPath>
    </resource>
</resources>

2) 在我的 MVC 配置中,我添加了这个(类似于下面的答案):

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }

添加这两个东西后,我的应用程序开始提供文件!

【讨论】:

  • 很高兴它为自己工作。我相信还有其他事情发生,因为这应该开箱即用。如果您仍然好奇,请发布指向重现此问题的 GitHub 存储库的链接。
【解决方案2】:

尝试将“类路径”添加到资源位置,

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

【讨论】:

    【解决方案3】:

    我相信你应该阅读这篇文章,如何提供静态内容:

    http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

    总而言之,您的浏览器正在缓存您的静态资源,例如 CSS 文件或 html 文件。

    为了打破这种行为,请先尝试清理浏览器缓存,在谷歌浏览器中进入设置,然后清除浏览数据。

    其次,将这些行添加到您的 application.properties 文件中以破坏缓存:

    spring.resources.chain.strategy.content.enabled=true
    spring.resources.chain.strategy.content.paths=/**
    

    或者添加这个:

    spring.resources.chain.strategy.fixed.enabled=true
    spring.resources.chain.strategy.fixed.paths=/**
    spring.resources.chain.strategy.fixed.version=v12
    

    【讨论】:

      【解决方案4】:

      在 Spring Boot 项目中,您可以通过扩展 WebMvcConfigurerAdapter 并覆盖 addResourceHandlers 来实现此功能,如下所示

          @SpringBootApplication
          public class DemoApplication extends WebMvcConfigurerAdapter {
      
              public static void main(String[] args) {
                  SpringApplication.run(DemoApplication.class, args);
              }
      
              @Override
              public void addResourceHandlers(ResourceHandlerRegistry registry) {
                  registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
              }   
          }
      

      可以通过http://localhost:8080/static/index.html访问索引页面

      【讨论】:

        猜你喜欢
        • 2023-04-10
        • 2015-03-06
        • 2018-06-10
        • 2017-05-29
        • 2021-12-20
        • 2018-05-19
        • 1970-01-01
        • 2019-02-19
        • 1970-01-01
        相关资源
        最近更新 更多