Spring Boot应用程序中多个Angular客户端的RequestMapping问题 [英] RequestMapping issue with multiple Angular clients in Spring Boot Application
问题描述
我需要处理一些GET请求才能转发到多个Angular客户端.
I need to handle some GET requests in order to forward to multiple Angular clients.
https://example.com/web/index.html // Client 1
https://example.com/admin/index.html // Client 2
由于我不想为/web
使用碎片化的(#
-ed)路径,所以事情变得相当糟糕.
Since I don't want to use fragmented (#
-ed) paths for /web
things get rather annyoing.
这是我当前的不起作用解决方案:
This is my current not working solution:
@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";
}
}
有了这个,有效的工作就是访问例如
With this, what does work is accessing e.g.
-
/web/pricing
但是无法访问
-
/web/pricing/vienna
我可以通过浏览器访问/web/pricing
,点击"刷新",一切正常.但这不适用于/web/pricing/vienna
.
I can access /web/pricing
via browser, hit "refresh" and everything will work. But it does not for /web/pricing/vienna
.
现在,我无法弄清楚如何处理请求以及如何转发请求,以使像/web/pricing/vienna
这样的子路径也能正常工作.
Now, I cannot figure out how to handle the requests and how to forward them in order to make sub-paths like /web/pricing/vienna
to work as well.
有什么办法可以使这项工作完成?
Is there any way I can make this work?
如果将@RequestMapping
路径更改为类似/web/**
的内容,则整个过程将陷入无休止的循环并破坏服务器.
If I change the @RequestMapping
path to something like /web/**
the whole thing ends up in an endless loop and breaks the server.
我可能需要的是这样的表达式:
What I probably need is an expression like this:
/web(/[^\\.]*)
这将导致
MATCH: /web/pricing
MATCH: /web/specials/city
MATCH: /web/specials/city/street
NO MATCH: /web/index.html
但是,Spring不喜欢此正则表达式:/web(/[^\\.]*)
However, Spring does not like this regex: /web(/[^\\.]*)
最后,这个问题归结为找到一种方法来匹配除/web
以下的静态资源以外的所有其他事物.
In the end this issue boils down to finding a way to match everythign except static resources below /web
.
推荐答案
这就是我最终要做的事情:
Here is what I ended up doing:
我将两个客户端都移到了子目录a/
:
I moved both clients to a subdirectory a/
:
static/a/web
static/a/admin
此外,我实现了这样的ForwardController
:
Further, I implemented a ForwardController
like this:
@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";
}
}
这篇关于Spring Boot应用程序中多个Angular客户端的RequestMapping问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!