【问题标题】:Spring Boot map URL request to a Static Resource, not serving index.html by defaultSpring Boot 将 URL 请求映射到静态资源,默认情况下不提供 index.html
【发布时间】:2018-01-02 18:06:20
【问题描述】:

我有一个 spring boot 应用程序在我的静态 [resources/static] 文件夹中提供角度资源。我也在使用同一个项目来服务我的 JSON-REST-API 端点。

因此我在 localhost:9090/api/... 下定义了我的 REST api 我的 Angular2 应用程序构建通过静态资源在 localhost:9090/ui/... 下提供

现在我想将我所有的 ui/** url 转发到 ui/index.html/** 我该怎么做?

附:我已经介绍了一个自定义静态资源 url 模式 spring.mvc.static-path-pattern=/ui/** 然后我所有的 ui/** 请求将查看静态/** 这样我就能够保护我的 /api/** 和“permitAll” ui/** 请求

【问题讨论】:

    标签: java spring angular rest spring-mvc


    【解决方案1】:

    这个简单的配置可以解决问题,如果启用,它甚至会刷新你的 # 个 angular 2 路由。

    @Configuration
    public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/ui").setViewName("forward:/ui/index.html");
            registry.addViewController("/ui/").setViewName("forward:/ui/index.html");
            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        }
    
    
    }
    

    指针: - @SpringBootApplication 应该在 Spring Boot 应用程序的运行位置进行注释 - 不要使用@EnableWebMvc,因为这会破坏 Spring Boot 完成的所有自动配置 - 查看此配置是否在您标记的目录下
    @ComponentScan({"com.foo.config"}) - 此实现专门针对您定义了自定义 spring.mvc.static-path-pattern [ui/]

    的情况

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2014-04-13
      • 2016-05-04
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多