本文最后更新于:2025年2月18日 下午
获取springboot所有接口
EndpointScanner.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.mvc.method.RequestMappingInfo;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import javax.annotation.Resource;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Map;import java.util.Set;@Component public class EndpointScanner { @Resource private ApplicationContext applicationContext; public void scanAndWriteEndpointsToFile (String filePath) throws IOException { RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); StringBuilder endpoints = new StringBuilder (); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) { RequestMappingInfo mappingInfo = entry.getKey(); HandlerMethod handlerMethod = entry.getValue(); Set<String> patterns = mappingInfo.getPatternsCondition().getPatterns(); Set<RequestMethod> methods = mappingInfo.getMethodsCondition().getMethods(); String controllerClass = handlerMethod.getBeanType().getName(); String methodName = handlerMethod.getMethod().getName(); for (String pattern : patterns) { if (pattern.contains("{" )){ String substring = pattern.substring(0 , pattern.indexOf("{" )); if (methods.isEmpty()) { endpoints.append("ANY " ).append(substring).append(" -> " ).append(controllerClass) .append("#" ).append(methodName) .append("\n" ); } else { for (RequestMethod method : methods) { endpoints.append(method.name()).append(" " ).append(substring).append(" -> " ).append(controllerClass) .append("#" ).append(methodName) .append("\n" ); } } }else { if (methods.isEmpty()) { endpoints.append("ANY " ).append(pattern).append(" -> " ).append(controllerClass) .append("#" ).append(methodName) .append("\n" ); } else { for (RequestMethod method : methods) { endpoints.append(method.name()).append(" " ).append(pattern).append(" -> " ).append(controllerClass) .append("#" ).append(methodName) .append("\n" ); } } } } } File file = new File (filePath); try (BufferedWriter writer = new BufferedWriter (new FileWriter (file))) { writer.write(endpoints.toString()); } } }
调用接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.io.IOException;@Component public class EndpointLoader { @Autowired private EndpointScanner endpointScanner; @PostConstruct public void init () throws IOException { String filePath = System.getProperty("user.dir" ) + "/secret/access_api_list.txt" ; endpointScanner.scanAndWriteEndpointsToFile(filePath); } }
输出示例:
假设接口:
1 2 3 4 5 6 7 8 9 @GetMapping("/api/users") public String getUsers () { ... }@PostMapping("/api/users") public String createUser () { ... }@RequestMapping("/api/products") public String handleProducts () { ... }
文件存储:【请求类型】 【请求路径】 -> 【所在类】【方法名】
1 2 3 GET /api/users -> com.example.controller.UserController#getUsers POST /api/users -> com.example.controller.UserController#createUser ANY /api/products -> com.example.controller.ProductController#handleProducts
可以视情况选择
例如只获取请求方法类型和请求路径
EndpointScanner.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.mvc.method.RequestMappingInfo;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import javax.annotation.Resource;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Map;import java.util.Set;@Component public class EndpointScanner { @Resource private ApplicationContext applicationContext; public void scanAndWriteEndpointsToFile (String filePath) throws IOException { RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); StringBuilder endpoints = new StringBuilder (); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) { RequestMappingInfo mappingInfo = entry.getKey(); Set<String> patterns = mappingInfo.getPatternsCondition().getPatterns(); Set<RequestMethod> methods = mappingInfo.getMethodsCondition().getMethods(); for (String pattern : patterns) { if (pattern.contains("{" )){ String substring = pattern.substring(0 , pattern.indexOf("{" )); if (methods.isEmpty()) { endpoints.append("ANY " ).append(substring).append("\n" ); } else { for (RequestMethod method : methods) { endpoints.append(method.name()).append(" " ).append(substring).append("\n" ); } } }else { if (methods.isEmpty()) { endpoints.append("ANY " ).append(pattern).append("\n" ); } else { for (RequestMethod method : methods) { endpoints.append(method.name()).append(" " ).append(pattern).append("\n" ); } } } } } File file = new File (filePath); try (BufferedWriter writer = new BufferedWriter (new FileWriter (file))) { writer.write(endpoints.toString()); } } }