Created
October 24, 2017 13:22
-
-
Save wlievens/2d367eaf4a52e25e1b6aceeaa4d73169 to your computer and use it in GitHub Desktop.
Spring MVC - Getting a hold of all endpoint patterns and converting them to regexps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void init(FilterConfig filterConfig) throws ServletException | |
{ | |
List<String> urlPatterns = requestMappingHandlerMapping.getHandlerMethods().keySet().stream() | |
.map(info -> info.getPatternsCondition().getPatterns().iterator().next()) | |
.filter(pattern -> pattern.contains("{")) | |
.distinct() | |
.collect(toList()); | |
endpoints = new Endpoint[urlPatterns.size()]; | |
for (int index = 0; index < urlPatterns.size(); index++) | |
{ | |
String path = urlPatterns.get(index); | |
String expression = path.replaceAll("\\{([a-zA-Z]+)\\}", "([^/]+)"); | |
System.out.println(path + "\t" + expression); | |
endpoints[index] = new Endpoint(path, Pattern.compile(expression)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment