springboot获取项目中所有对外提供的接口信息
@Component
public class Test implements ApplicationRunner {
@Resource
private ApplicationContext applicationContext;
@Override
public void run(ApplicationArguments args) throws Exception {
RequestMappingHandlerMapping mappings = applicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodHashMap = new HashMap<>(mappings.getHandlerMethods());
requestMappingInfoHandlerMethodHashMap.forEach((requestMappingInfo, handlerMethod) -> {
Set<RequestMethod> requestType = new HashSet<>(requestMappingInfo.getMethodsCondition().getMethods());
StringJoiner type = new StringJoiner(",");
for (RequestMethod requestMethod : requestType) {
type = type.add(requestMethod.name());
}
String url = new ArrayList<>(requestMappingInfo.getPatternsCondition().getPatterns()).get(0);
// 全类名 + 方法名
System.out.print(new StringBuilder(handlerMethod.getBeanType().getName()).append(".").append(handlerMethod.getMethod().getName()).toString());
// 请求地址
System.out.print("\t"+url);
// 请求方式
System.out.println("\t"+type.toString());
});
}
}