java - Find which url was used to access the controller when multiple url mapping to the same controller method
问题描述
I see Spring MVC multiple url mapping to the same controller method
So now I have a method defined as
@RequestMapping(value = {"/aaa", "/bbb", "/ccc/xxx"}, method = RequestMethod.POST)
public String foo() {
// was it called from /aaa or /bbb
}
At run time, I want to know if the controller was called from /aaa
or /bbb
解决方案
您可以使用HttpServletRequest#getServletPath
哪个:
返回此请求的 URL 中调用 servlet 的部分。此路径以“/”字符开头,包括 servlet 名称或 servlet 路径,但不包括任何额外的路径信息或查询字符串。
如下:
@RequestMapping(value = {"/aaa", "/bbb", "/ccc/xxx"}, method = RequestMethod.POST)
public String foo(HttpServletRequest request) {
String path = request.getServletPath(); // -> gives "/aaa", "/bbb" or "/ccc/xxx"
}
推荐阅读
- c# - 单元测试 - 方法测试失败(labeltext.visible)
- javascript - Next.js 在 dist 文件夹中创建一个 polyfill.js 文件
- sql - SQL:在给定的代码中,为什么我们的子查询从未使用或显示时给定别名
- scala - 是否有任何通用解决方案可以处理在 chisel3 中添加相同的泛型类型添加?
- javascript - Formik 不会在控制台上记录值
- java - Intellij Tool:一次自动导入所有java类的捷径是什么?
- android - 不使用 SpannableString 更改文本颜色
- firebase - Firebase 是否捕获应用程序的内存和 CPU 利用率?
- android - 如何使用 mockito 为“const val”编写 Junit 测试
- android - 如何在 3 分钟的活动中每个用户至少一次在应用程序中显示应用评分弹出窗口?