查询mapping是否存在
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
public RequestMappingInfo getRequestMappingInfo(String pattern, String method) {
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
for (RequestMappingInfo info : map.keySet()) {
Set<String> patterns = MappingInfoUtils.getPatterns(info);
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
if (patterns.contains(pattern) && (methods.isEmpty() || methods.contains(RequestMethod.valueOf(method)))){
return info;
}
}
return null;
}
/**
* 注册mapping
*
* @param apiInfo
*/
public synchronized void registerMappingForApiInfo(ApiInfo apiInfo) throws NoSuchMethodException {
if (ApiType.Code.name().equals(apiInfo.getType())) {
return;
}
String pattern = apiInfo.getFullPath();
RequestMappingInfo mappingInfo = getRequestMappingInfo(pattern,apiInfo.getMethod());
if (mappingInfo != null){
return;
}
log.debug("Mapped [{}]{}", apiInfo.getMethod(), pattern);
mappingInfo = RequestMappingInfo.paths(pattern)
.methods(RequestMethod.valueOf(apiInfo.getMethod()))
.options(this.config)
.build();
Method targetMethod = QLRequestMappingFactory.class.getDeclaredMethod("execute", Map.class, Map.class, HttpServletRequest.class, HttpServletResponse.class);
requestMappingHandlerMapping.registerMapping(mappingInfo, mappingFactory, targetMethod);
}
public class MappingInfoUtils {
public static Set<String> getPatterns(RequestMappingInfo info){
return info.getPatternsCondition() == null?info.getPathPatternsCondition().getPatternValues():info.getPatternsCondition().getPatterns();
}
}