Skip to content

Instantly share code, notes, and snippets.

@wlievens
Created October 24, 2017 13:22
Show Gist options
  • Save wlievens/2d367eaf4a52e25e1b6aceeaa4d73169 to your computer and use it in GitHub Desktop.
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
@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