簡體   English   中英

Spring Boot映射URL請求到靜態資源,默認情況下不提供index.html

[英]Spring Boot map URL request to a Static Resource, not serving index.html by default

我有一個spring boot應用程序,在我的靜態[resources / static]文件夾中提供角度資源。 我還使用同一項目來服務我的JSON-REST-API端點。

因此,我已經在localhost:9090 / api /下定義了我的REST api。我的Angular2應用程序構建是通過靜態資源在localhost:9090 / ui /下提供的。

現在,我想將我的所有ui / ** URL轉發到ui / index.html / **,我該怎么做?

PS我引入了一個自定義的靜態資源URL模式spring.mvc.static-path-pattern=/ui/**然后,我的所有ui / **請求都將查找為static / **這樣,我便可以保護我的/ api / **和“ permitAll” ui / **請求

這個簡單的配置可以解決這個問題,如果啟用,甚至可以刷新角度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);
    }


}

指針:-在運行Spring Boot應用程序的地方應注明@SpringBootApplication-不要使用@EnableWebMvc,因為這會破壞Spring Boot完成的所有自動配置-查看此配置是否在您標記為的目錄下
@ComponentScan({“ com.foo.config”})-此實現專為您定義了自定義spring.mvc.static-path-pattern [ui /]的情況而量身定制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM