我正在为基于react的单页应用程序开发一个spring后端,其中我使用react-router进行客户端路由。
除了index.html页面之外,后端还提供路径上的数据/api/**
.
为了提供我的index.htmlsrc/main/resources/public/index.html
在根路径上/
我的应用程序添加了一个资源处理程序
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/").addResourceLocations("/index.html");
}
我想要的是在没有其他路由匹配时提供 index.html 页面,例如当我调用除/api
.
如何在 Spring 中配置这样的包罗万象的路由?
由于我的反应应用程序可以使用根作为转发目标,这最终对我有用
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/**/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
.setViewName("forward:/");
}
}
老实说,我不知道为什么它必须完全采用这种特定格式以避免无限转发循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)