执行请求时如何获取Controller层配置的当前请求映射URL? [英] How to get the current Request Mapping URL configured at Controller layer when request is executed?
问题描述
我浏览了很多链接,例如 如何在视图中显示所有控制器和映射 和 如何为所有控制器配置默认的@RestController URI 前缀? 等等.
I went through so many links like How to show all controllers and mappings in a view and How to configure a default @RestController URI prefix for all controllers? and so on.
我想在过滤器拦截器处获取请求映射 URL
I want to get the Request Mapping URL at Filter interceptor
例如:这个URL是我在REST控制器方法中配置的,自然我们会通过/employees/employee-names/John
来获取Employee John.
Ex: This URL I configured at REST controller method, and naturally we will pass /employees/employee-names/John
to get the Employee John.
/employees/employee-names/{employee_name}
现在,当有人点击 /employees/employee-names/John
我想获得实际映射 url 的值,如果 REST 控制器 /employees/employee-names/{employee_name}
,
Now, when somebody hit /employees/employee-names/John
I want to get the value of actual mapping url if REST controller /employees/employee-names/{employee_name}
,
任何指示如何获得?
推荐答案
我能够使用下面的代码解决这个问题.AntPathMatcher
是识别传入请求和您在属性文件中配置的 URL 是否完全匹配的完美方式.这个解决方案对我很有用.
I was able to solve this issue using below code. AntPathMatcher
is the perfect way to identify if the incoming request and URL you configured in the property file matches exactly. This solution works greatly for me.
AntPathMatcher springMatcher = new AntPathMatcher();
Optional<String> antMatch = props.getMapping().stream()
.filter(//Perform Some Filter as per need)
.map(Mapping::getVersion)
.findFirst();
return antMatch.isPresent() ? antMatch.get() : null;
这篇关于执行请求时如何获取Controller层配置的当前请求映射URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!