Spring boot 如何为所有扩展类型请求创建控制器?

Spring boot 如何为所有扩展类型请求创建控制器?,spring-boot,Spring Boot,具有spring启动项目和默认控制器: @Controller public class GenericController { @RequestMapping(value= {"/**.html", "/"}) public String httpRequest(Model model, HttpServletRequest request) { @RequestMapping(value = "/some/path/to/your/thingy", method =

具有spring启动项目和默认控制器:

@Controller
public class GenericController
{
    @RequestMapping(value= {"/**.html", "/"})
    public String httpRequest(Model model, HttpServletRequest request)
    {
@RequestMapping(value = "/some/path/to/your/thingy", method = RequestMethod.GET)
public ResponseEntity<Object> aMethod() throws Exception {

    return ResponseEntity.ok("ok");
}
但仅适用于/*.html路由。如何捕获任何文件夹源的所有.html路由?例如:
/abc.html
/abc/def.html
/abc/def/ghi.html
,等等

我了解到:

并尝试:

@RequestMapping(value= {"/**/*.html", "/"})

但当调用返回http状态404时,它不起作用。

我不知道您为什么要这样做,但您可以通过破解
路径参数来帮您完成。但这是一种肮脏的方式,可能会与其他
映射发生冲突

通过使用下面这样的路径参数,您可以执行以下操作:
/abc.html
/abc/def.html
/abc/def/ghi.html

@RequestMapping(value = { "/**.html" , "/{path2}/**.html" ,"/{path}/{path2}/**.html" })
public String httpRequest(Model model) {
    //You can also check which path variables are present and work accordingly 
    System.out.println("index");
    return "index";
}

如果您想为API创建一个单一入口点,那么我建议您阅读

另一种方法是使用过滤器,根据传入的URI重定向您的响应:

@Component
@Order(1)
public class AFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        if(httpServletRequest.getRequestURI()...){ // use regex ?
            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

            ((HttpServletResponse) servletResponse).sendRedirect("/some/path/to/your/thingy");
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}
和一些控制器:

@Controller
public class GenericController
{
    @RequestMapping(value= {"/**.html", "/"})
    public String httpRequest(Model model, HttpServletRequest request)
    {
@RequestMapping(value = "/some/path/to/your/thingy", method = RequestMethod.GET)
public ResponseEntity<Object> aMethod() throws Exception {

    return ResponseEntity.ok("ok");
}
@RequestMapping(value=“/some/path/to/your/thingy”,method=RequestMethod.GET)
公共响应method()引发异常{
返回响应。ok(“ok”);
}