Java书签 #获取项目应用中所有Mapper接口并打印URL

1. 今日书签

获取项目应用中所有Mapper接口并打印URL。

2. 实现案例

@Component
public class HttpUrlPrinter {

    private final RequestMappingHandlerMapping handlerMapping;

    @Autowired
    public HttpUrlPrinter(RequestMappingHandlerMapping handlerMapping) {
        this.handlerMapping = handlerMapping;
    }

    @PostConstruct
    public 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);
            });
        }
    }
}

你可能感兴趣的:(Java,1024程序员节,java)