1. 今日书签
获取项目应用中所有Mapper接口并打印URL。
2. 实现案例
@Component
public class HttpUrlPrinter {private final RequestMappingHandlerMapping handlerMapping;@Autowiredpublic HttpUrlPrinter(RequestMappingHandlerMapping handlerMapping) {this.handlerMapping = handlerMapping;}@PostConstructpublic void printHttpUrls() {// 获取所有处理程序方法及其映射信息Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {RequestMappingInfo mappingInfo = entry.getKey();HandlerMethod handlerMethod = entry.getValue();// 获取映射的URL模式mappingInfo.getPatternsCondition().getPatterns().forEach(pattern -> {// 获取HTTP请求方法(默认为GET)String httpMethod = mappingInfo.getMethodsCondition().getMethods().isEmpty()? "GET": mappingInfo.getMethodsCondition().getMethods().iterator().next().toString();// 获取处理程序方法所在的类名和方法名String className = handlerMethod.getBeanType().getName();String methodName = handlerMethod.getMethod().getName();// 打印HTTP方法、URL模式、处理程序类名和方法名System.out.println(httpMethod + " " + pattern + " => " + className + "#" + methodName);});}}
}