代码之家  ›  专栏  ›  技术社区  ›  Stefan Falk

Spring引导应用程序中多角度客户端的请求映射问题

  •  0
  • Stefan Falk  · 技术社区  · 6 年前

    我需要处理一些GET请求,以便转发给多角度客户。

    https://example.com/web/index.html    // Client 1
    https://example.com/admin/index.html  // Client 2
    

    因为我不想使用碎片( # -ED路径 /web 事情变得相当有意义。

    这是我的电流 不工作 解决方案:

    @Controller
    public class ForwardController {
    
        @RequestMapping(value = "/*", method = RequestMethod.GET)
        public String redirectRoot(HttpServletRequest request) {
    
            String req = request.getRequestURI();
    
            if (req.startsWith("/admin/")) {
                return this.redirectAdminTool(request);
            }
    
            return "forward:/web/index.html";
        }
    
        @RequestMapping(value = "/web/{path:[^.]*}", method = RequestMethod.GET)
        public String redirectWeb(HttpServletRequest request) {
            return "forward:/web/index.html";
        }
    
        @RequestMapping(value = "/admin/{path:[^.]*}", method = RequestMethod.GET)
        public String redirectAdminTool(HttpServletRequest request) {
            return "forward:/admin/index.html";
        }
    }
    

    有了这个,什么工作是访问,例如

    • /web/pricing

    但不起作用的是访问

    • /web/pricing/vienna

    我可以访问 网络/定价 通过浏览器,点击“ 刷新 “一切都会好起来的。但不是为了 /网络/定价/维也纳 .

    现在,我不知道如何处理请求,以及如何转发请求,以使子路径像 /网络/定价/维也纳 工作也一样。

    我有什么办法可以做到这一点吗?

    如果我改变 @RequestMapping 通往某种事物的路 /web/** 整个过程以一个无休止的循环结束,并破坏了服务器。


    我可能需要这样的表达:

    /web(/[^\\.]*)
    

    会导致

    MATCH:    /web/pricing
    MATCH:    /web/specials/city
    MATCH:    /web/specials/city/street
    NO MATCH: /web/index.html
    

    但是,Spring不喜欢这个regex: /web(/[^\\.]*)

    最后,这个问题归结为找到一种方法来匹配除下面的静态资源之外的所有签名。 网络 .

    2 回复  |  直到 6 年前
        1
  •  0
  •   Stefan Falk    6 年前

    我最后做的是:

    我把两个客户都转移到一个子目录 a/ :

    static/a/web
    static/a/admin
    

    此外,我还实现了 ForwardController 这样地:

    @Controller
    public class ForwardController {
    
        @RequestMapping(value = "/*", method = RequestMethod.GET)
        public String redirectRoot(HttpServletRequest request) {
            return "forward:/a/web/index.html";
        }
    
        @RequestMapping(value = "/a/**/{path:[^.]*}", method = RequestMethod.GET)
        public String redirectClients(HttpServletRequest request) {
    
            String requestURI = request.getRequestURI();
    
            if (requestURI.startsWith("/a/admin/")) {
                return "forward:/a/admin/index.html";
            }
    
            return "forward:/a/web/index.html";
        }
    
    }
    
        2
  •  -1
  •   Raphael Alves    6 年前

    子路由将不起作用,因为要获取所有路由,需要先加载主索引。 解决方法是每当服务器端找不到路径时重定向到主索引:

    @Controller
    @RequestMapping("/error")
    public class ErrorHandlerController extends AbstractErrorController {
    
    private static final String ROOT_PATH = "/";
    
    public ErrorHandlerController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }
    
    @RequestMapping
    public void errorHtml(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        if(HttpStatus.NOT_FOUND.equals(getStatus(request))) {
            response.sendRedirect(ROOT_PATH);
        }
    }
    
    @Override
    public String getErrorPath() {
        return "error";
    }
    
    }