Path prefixes for groups of controllers

Issue: SPR-16336
This commit is contained in:
Rossen Stoyanchev
2018-06-07 18:16:18 -04:00
parent 31159a8506
commit 19dc981685
12 changed files with 313 additions and 33 deletions

View File

@@ -16,8 +16,12 @@
package org.springframework.web.servlet.config.annotation;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.lang.Nullable;
import org.springframework.util.PathMatcher;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.util.UrlPathHelper;
/**
@@ -53,6 +57,9 @@ public class PathMatchConfigurer {
@Nullable
private PathMatcher pathMatcher;
@Nullable
private Map<String, HandlerTypePredicate> pathPrefixes;
/**
* Whether to use suffix pattern match (".*") when matching patterns to
@@ -110,6 +117,22 @@ public class PathMatchConfigurer {
return this;
}
/**
* Configure a path prefix to apply to matching controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate}. The prefix for the first matching predicate
* is used.
* @param prefix the prefix to apply
* @param predicate a predicate for matching controller types
* @since 5.1
*/
public PathMatchConfigurer addPathPrefix(String prefix, HandlerTypePredicate predicate) {
this.pathPrefixes = this.pathPrefixes == null ? new LinkedHashMap<>() : this.pathPrefixes;
this.pathPrefixes.put(prefix, predicate);
return this;
}
@Nullable
public Boolean isUseSuffixPatternMatch() {
@@ -136,4 +159,8 @@ public class PathMatchConfigurer {
return this.pathMatcher;
}
@Nullable
protected Map<String, HandlerTypePredicate> getPathPrefixes() {
return this.pathPrefixes;
}
}

View File

@@ -70,6 +70,7 @@ import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.method.support.CompositeUriComponentsContributor;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
@@ -284,15 +285,18 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
mapping.setCorsConfigurations(getCorsConfigurations());
PathMatchConfigurer configurer = getPathMatchConfigurer();
Boolean useSuffixPatternMatch = configurer.isUseSuffixPatternMatch();
Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch();
Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
if (useSuffixPatternMatch != null) {
mapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
}
Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch();
if (useRegisteredSuffixPatternMatch != null) {
mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch);
}
Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
if (useTrailingSlashMatch != null) {
mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
}
@@ -307,6 +311,11 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
mapping.setPathMatcher(pathMatcher);
}
Map<String, HandlerTypePredicate> pathPrefixes = configurer.getPathPrefixes();
if (pathPrefixes != null) {
mapping.setPathPrefixes(pathPrefixes);
}
return mapping;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,10 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
@@ -28,12 +31,14 @@ import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
import org.springframework.web.servlet.handler.RequestMatchResult;
@@ -62,6 +67,8 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
private boolean useTrailingSlashMatch = true;
private final Map<String, HandlerTypePredicate> pathPrefixes = new LinkedHashMap<>();
private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();
@Nullable
@@ -102,6 +109,22 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
this.useTrailingSlashMatch = useTrailingSlashMatch;
}
/**
* Configure path prefixes to apply to controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate} in the map. The prefix for the first matching
* predicate is used, assuming the input map has predictable order.
* @param prefixes a map with path prefixes as key
* @since 5.1
*/
public void setPathPrefixes(Map<String, HandlerTypePredicate> prefixes) {
this.pathPrefixes.clear();
prefixes.entrySet().stream()
.filter(entry -> StringUtils.hasText(entry.getKey()))
.forEach(entry -> this.pathPrefixes.put(entry.getKey(), entry.getValue()));
}
/**
* Set the {@link ContentNegotiationManager} to use to determine requested media types.
* If not set, the default constructor is used.
@@ -151,6 +174,14 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
return this.useTrailingSlashMatch;
}
/**
* The configured path prefixes as a read-only, possibly empty map.
* @since 5.1
*/
public Map<String, HandlerTypePredicate> getPathPrefixes() {
return Collections.unmodifiableMap(this.pathPrefixes);
}
/**
* Return the configured {@link ContentNegotiationManager}.
*/
@@ -195,6 +226,16 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
if (typeInfo != null) {
info = typeInfo.combine(info);
}
for (Map.Entry<String, HandlerTypePredicate> entry : this.pathPrefixes.entrySet()) {
if (entry.getValue().test(handlerType)) {
String prefix = entry.getKey();
if (this.embeddedValueResolver != null) {
prefix = this.embeddedValueResolver.resolveStringValue(prefix);
}
info = RequestMappingInfo.paths(prefix).build().combine(info);
break;
}
}
}
return info;
}