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

老子叫甜甜 提交于 2020-03-21 06:52:07

问题


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.

I want to get the Request Mapping URL at Filter interceptor

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}

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},

Any pointers how to get that ?


回答1:


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;



回答2:


Spring MVC sets the attribute HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, which you can use to get the pattern that was used to match the incoming request:

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

That would return /employees/employee-names/{employee_name} in your case.



来源:https://stackoverflow.com/questions/60446807/how-to-get-the-current-request-mapping-url-configured-at-controller-layer-when-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!