[英]How to specify all routes except one?
I'm trying to overwrite my old project from native JS to React with MobX. 我正在尝试将我的旧项目从本机JS覆盖到React与MobX。 My application has several pages and I want to use react-router to handle them. 我的应用程序有几个页面,我想使用react-router来处理它们。 For that I need to return index.html page for all routes from my Spring-boot controller: 为此我需要从我的Spring-boot控制器返回所有路由的index.html页面:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RoutesController {
@RequestMapping(value = { "", "/**" }, method = RequestMethod.GET)
public String index() {
return "index";
}
}
Also I have special folder for static components on embedded Apache Tomcat server. 此外,我在嵌入式Apache Tomcat服务器上有静态组件的特殊文件夹。 This folder contains build.js for my client-side application. 此文件夹包含我的客户端应用程序的build.js. Controller presented above can serve all requests to server with index.html page, but I have also client-side application on /static/**
route. 上面提到的控制器可以使用index.html页面为服务器提供所有请求,但我在/static/**
路由上也有客户端应用程序。 How can I specify all routes in my controller except /static/**
? 除了/static/**
之外,如何指定控制器中的所有路径?
I solved my problem. 我解决了我的问题。 To get only one page for all requests your controller must implement ErrorController
and two methods: error
and getErrorPath
. 要为所有请求只获取一个页面,您的控制器必须实现ErrorController
和两个方法: error
和getErrorPath
。 Here is an example: 这是一个例子:
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RoutesController implements ErrorController {
private static final String PATH = "/error";
@RequestMapping(value = PATH)
public String error() {
return "index";
}
@Override
public String getErrorPath() {
return PATH;
}
}
Server get the request and will not find any routes, so it will return result of error
method. 服务器获取请求并且不会找到任何路由,因此它将返回error
方法的结果。
Maybe that's not the best solution, but after three days of searching the Internet I have not found anything better. 也许这不是最好的解决方案,但经过三天的互联网搜索,我找不到更好的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.