Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何指定除一个路由之外的所有路由?_Java_Spring_Spring Boot - Fatal编程技术网

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

Java 如何指定除一个路由之外的所有路由?,java,spring,spring-boot,Java,Spring,Spring Boot,我正试图从原生JS覆盖我的旧项目,以对MobX作出反应。我的应用程序有几个页面,我想使用react路由器来处理它们。为此,我需要从Spring boot controller返回所有路由的index.html页面: import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.we

我正试图从原生JS覆盖我的旧项目,以对MobX作出反应。我的应用程序有几个页面,我想使用react路由器来处理它们。为此,我需要从Spring boot controller返回所有路由的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";
    }
}
另外,我在嵌入式ApacheTomcat服务器上有一个用于静态组件的特殊文件夹。此文件夹包含客户端应用程序的build.js。 上面介绍的控制器可以通过index.html页面向服务器提供所有请求,但我还有
/static/**
路由上的客户端应用程序。
如何在控制器中指定除
/static/**
之外的所有路由?

我解决了问题。要为所有请求仅获取一个页面,控制器必须实现
ErrorController
和两种方法:
error
getErrorPath
。 以下是一个例子:

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;
    }
}
服务器收到请求后,将找不到任何路由,因此它将返回
错误
方法的结果


也许这不是最好的解决办法,但在网上搜索了三天之后,我没有找到更好的办法。

我解决了我的问题。要为所有请求仅获取一个页面,控制器必须实现
ErrorController
和两种方法:
error
getErrorPath
。 以下是一个例子:

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;
    }
}
服务器收到请求后,将找不到任何路由,因此它将返回
错误
方法的结果


也许这不是最好的解决方案,但经过三天的互联网搜索,我没有找到更好的解决方案。

如果使用Spring Boot,静态资源应该由它自动处理。你有什么问题吗?@dunni谢谢你的回复!不,我对静态资源没有问题。唯一的问题是我不知道如何配置路由处理。如果使用Spring Boot,静态资源应该由它自动处理。你有什么问题吗?@dunni谢谢你的回复!不,我对静态资源没有问题。唯一的问题是我不知道如何配置路由的处理。