问题
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