Created
April 12, 2017 02:23
-
-
Save zhoulifu/bfbbb08e4e65773a6dee4c4ac3003ef7 to your computer and use it in GitHub Desktop.
Mapping multi-path to a handler method
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
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import org.springframework.beans.factory.InitializingBean; | |
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; | |
import org.springframework.web.servlet.mvc.method.RequestMappingInfo; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
@Configuration | |
public class ExtendPathMapping extends WebMvcRegistrationsAdapter implements | |
InitializingBean { | |
private Map<String, String> legacyMapping; // mapping legacy --> current | |
@Override | |
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { | |
return new CustomRequestMappingHandlerMapping(); | |
} | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
HashMap<String, String> mapping = new HashMap<>(); | |
mapping.put("/foo/quz", "/bar/quz"); | |
this.legacyMapping = Collections.unmodifiableMap(mapping); | |
} | |
private class CustomRequestMappingHandlerMapping extends | |
RequestMappingHandlerMapping { | |
@Override | |
protected void registerHandlerMethod(Object handler, Method method, | |
RequestMappingInfo mapping) { | |
super.registerHandlerMethod(handler, method, withLegacyMapping(mapping)); | |
} | |
private RequestMappingInfo withLegacyMapping(RequestMappingInfo mapping) { | |
List<String> patterns = combineLegacyPattern( | |
mapping.getPatternsCondition().getPatterns()); | |
PatternsRequestCondition prc = new PatternsRequestCondition( | |
patterns.toArray(new String[patterns.size()])); | |
return new RequestMappingInfo(prc, mapping.getMethodsCondition(), | |
mapping.getParamsCondition(), | |
mapping.getHeadersCondition(), | |
mapping.getConsumesCondition(), | |
mapping.getProducesCondition(), | |
mapping.getCustomCondition()); | |
} | |
private List<String> combineLegacyPattern(Set<String> patterns) { | |
List<String> result = new ArrayList<>(patterns); | |
for (String pattern : patterns) { | |
for (Map.Entry<String, String> entry : legacyMapping.entrySet()) { | |
String legacy = entry.getKey(); | |
if (getPathMatcher().match(pattern, entry.getValue())) { | |
result.add(legacy); | |
} | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment