gpt4 book ai didi

java - Spring Boot 将 URL 请求映射到静态资源,默认情况下不提供 index.html

转载 作者:行者123 更新时间:2023-11-30 06:35:04 24 4
gpt4 key购买 nike

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

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

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

附注我引入了自定义静态资源 url 模式 spring.mvc.static-path-pattern=/ui/**然后我所有的 ui/** 请求都会查找 static/**这样我就能够保护我的/api/** 和“permitAll”ui/** 请求

最佳答案

这个简单的配置就可以解决问题,如果启用的话,甚至可以刷新 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/]

关于java - Spring Boot 将 URL 请求映射到静态资源,默认情况下不提供 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45343524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com