oli*_*oli 30 spring spring-mvc catch-all single-page-application spring-boot
我正在开发一个基于反应的单页面应用程序的弹簧后端,我正在使用react-router进行客户端路由.
在index.html页面旁边,后端提供路径上的数据/api/**.
为了从我的应用程序src/main/resources/public/index.html的根路径上提供我的index.html,/我添加了一个资源处理程序
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/").addResourceLocations("/index.html");
}
Run Code Online (Sandbox Code Playgroud)
我想要的是在没有其他路由匹配的情况下提供index.html页面,例如当我调用除了以外的路径时/api.
如何在春季配置这种全能路线?
Pet*_*nen 28
由于我的反应应用程序可以使用root作为前向目标,因此最终为我工作
@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:/");
}
}
Run Code Online (Sandbox Code Playgroud)
说实话,我不知道为什么它必须完全采用这种特定格式才能避免无限转发循环.
Dan*_*tad 12
我在Spring Boot应用程序中托管了基于Polymer的PWA,以及图像之类的静态Web资源,以及"/ api/..."下的REST API.我希望客户端应用程序处理PWA的URL路由.这是我使用的:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* Ensure client-side paths redirect to index.html because client handles routing. NOTE: Do NOT use @EnableWebMvc or it will break this.
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// Map "/"
registry.addViewController("/")
.setViewName("forward:/index.html");
// Map "/word", "/word/word", and "/word/word/word" - except for anything starting with "/api/..." or ending with
// a file extension like ".js" - to index.html. By doing this, the client receives and routes the url. It also
// allows client-side URLs to be bookmarked.
// Single directory level - no need to exclude "api"
registry.addViewController("/{x:[\\w\\-]+}")
.setViewName("forward:/index.html");
// Multi-level directory path, need to exclude "api" on the first part of the path
registry.addViewController("/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}")
.setViewName("forward:/index.html");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/webapp/");
}
}
Run Code Online (Sandbox Code Playgroud)
这也适用于Angular和React应用程序.
避免@EnableWebMvc
默认情况下,Spring-Boot提供静态内容src/main/resources:
或者保留@EnableWebMvc并覆盖addViewControllers
你有没说明@EnableWebMvc?看看这个:Java Spring Boot:如何将我的应用程序根目录("/")映射到index.html?
要么删除@EnableWebMvc,要么可以重新定义addViewControllers:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
Run Code Online (Sandbox Code Playgroud)
或定义一个控制器来捕获 /
您可以在github上看一下这个spring-boot-reactjs示例项目:
它使用Controller做你想做的事:
@Controller
public class HomeController {
@RequestMapping(value = "/")
public String index() {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
它index.html是在src/main/resources/templates
小智 8
我在 Spring Boot 应用程序中使用了 react 和 react-router,这就像创建一个控制器一样简单,该控制器映射到/我的网站的子树,例如/users/**
Here is my solution
@Controller
public class SinglePageAppController {
@RequestMapping(value = {"/", "/users/**", "/campaigns/**"})
public String index() {
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
此控制器不会捕获 Api 调用,并且会自动处理资源。
| 归档时间: |
|
| 查看次数: |
20114 次 |
| 最近记录: |