如何在执行请求时获取当前Controller层配置的Request Mapping URL?

How to get the current Request Mapping URL configured at Controller layer when request is executed?

我浏览了很多链接,例如 How to show all controllers and mappings in a view and 等等。

我想在过滤器拦截器

处获取请求映射 URL

例如:这个URL我在REST控制器方法配置的,自然我们会通过/employees/employee-names/John来获取Employee John。

/employees/employee-names/{employee_name}

现在,当有人点击 /employees/employee-names/John 我想获取实际映射的值 url 如果 REST 控制器 /employees/employee-names/{employee_name}

任何指示如何得到它?

我能够使用以下代码解决此问题。 AntPathMatcher 是识别传入请求和您在 属性 文件中配置的 URL 是否完全匹配的完美方法。这个解决方案对我很有用。

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;

Spring MVC 设置属性 HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,您可以使用它来获取用于匹配传入请求的模式:

String matchingPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)

在你的情况下 return /employees/employee-names/{employee_name}