2

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 ?

2
  • please check it, stackoverflow.com/questions/14025872/… Commented Feb 28, 2020 at 7:51
  • Is there any way if we can get the controller method name ? Commented Feb 28, 2020 at 9:54

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not working for me. Best matching patterns gives null to me
@PAA: Maybe your filter is executed before the handler mapping is done and this attribute is set? Could you use a ControllerAdvice instead of a filter?
For future readers who stumble upon this answer, ((HttpServletRequest)req).getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)) works for me. But it must be used after chain.doFilter() when used in a filter.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.