简体   繁体   English

如何指定除一个以外的所有路由?

[英]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和两个方法: errorgetErrorPath 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.

相关问题 春季启动:将Servlet过滤器应用于除一条之外的所有路由 - Spring-boot: Apply Servlet Filter to all routes except one 如何捕获除特定异常之外的所有异常? - How to catch all exceptions except a specific one? 如何防止从一类以外的所有类导入软件包? - How to prevent imports of packages from all classes except one class? 如何更新实时数据库中除一个之外的所有值? - How to update all values except one in Realtime Database? 迭代时如何修改除当前元素以外的所有元素 - How to modify all elements except the current one when iterating 如何显示ArrayList中除一个元素外的所有元素? - How to display all elements in an ArrayList except one element? 在Eclipse Luna中,如何跳过除一个以外的所有断点? - In Eclipse Luna how to skip all breakpoints except one? 如何替换除一个用户输入字符串中的所有字符 - How to replace all characters in a user input string except one 如何停止池中除最后一个任务以外的所有任务? - How to stop all tasks in the pool except last one? 如何返回 JSONObject 中除“空”值以外的所有值? - How can I return all the values except for a "null" one in a JSONObject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM