SpringBoot 基于 CommandLineRunner 扩展点获取 web 项目所有 URL
Spring Boot 的 CommandLineRunner 接口是一个函数式接口,用于在 Spring Boot 应用程序启动后执行一些初始化操作。它提供了一个 run 方法,该方法在应用程序启动后被调用。使用 CommandLineRunner 接口,可以在应用程序启动后执行一些必要的初始化操作,例如加载初始化数据,启动打印应用信息,启动异步任务,接口健康检查,外部服务调用,启动参数校验等。可以通过实现 CommandLineRunner 接口,并重写 run 方法来定义自己的初始化逻辑。
需求点:
项目在启动的时候,会采集项目所有对外 http 接口信息,来做一些额外的信息,对此,我们基于 CommandLineRunner 在应用启动就上报这些信息给指定的平台
自定义实现 CommandLineRunner 函数接口
java
@Component
@Slf4j
public class RequestUrlEndpointCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("-----------------开始采集项目所有的http接口信息------------------------");
RequestMappingHandlerMapping handlerMapping = ApplicationContextUtils.getBean(RequestMappingHandlerMapping.class);
assert handlerMapping != null;
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
List<Map<String, String>> list = new ArrayList<>();
handlerMethods.forEach((requestMappingInfo , handlerMethod) -> {
Map<String, String> map = new HashMap<>();
PatternsRequestCondition condition = requestMappingInfo.getPatternsCondition();
if (condition != null) {
Set<String> patterns = condition.getPatterns();
patterns.forEach(pattern -> { map.put("url", pattern);});
}
map.put("className",handlerMethod.getMethod().getDeclaringClass().getName());
map.put("methodName",handlerMethod.getMethod().getName());
RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
map.put("type", requestMethod.toString());
}
list.add(map);
});
list.forEach(m -> {
log.info("className = {} methodName = {} request method = {} url = {} ", m.get("className"), m.get("methodName"),m.get("type"), m.get("url"));
});
log.info("-----------------采集项目所有的http接口信息------------------------");
}
}
启动
启动之后我们可以看到控制台输出以下信息
text
2024-10-15 14:03:44.356 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : -----------------开始采集项目所有的http接口信息------------------------
2024-10-15 14:03:44.356 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = springfox.documentation.oas.web.OpenApiControllerWebMvc methodName = getDocumentation request method = GET url = /v3/api-docs
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = com.ssn.hub.api.ProjectApi methodName = addProjectMember request method = POST url = /api/projects/add/member
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = springfox.documentation.swagger.web.ApiResourceController methodName = securityConfiguration request method = GET url = /swagger-resources/configuration/security
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = com.ssn.hub.api.ProjectApi methodName = addTask request method = POST url = /api/projects/add/task
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = com.ssn.hub.api.ProjectApi methodName = addProjectCollection request method = POST url = /api/projects/add/collection
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = com.ssn.hub.api.UserApi methodName = addUser request method = POST url = /api/user/add
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = springfox.documentation.swagger2.web.Swagger2ControllerWebMvc methodName = getDocumentation request method = GET url = /v2/api-docs
2024-10-15 14:03:44.357 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : className = com.ssn.hub.api.FileApi methodName = upload request method = POST url = /api/
.....
2024-10-15 14:03:44.358 INFO 26680 --- [ main] .c.i.RequestUrlEndpointCommandLineRunner : -----------------采集项目所有的http接口信息------------------------
总结
通过 CommandLineRunner,可以深度控制 Spring Boot 应用的启动流程,在应用启动阶段增强各种自定义逻辑。是 Spring Boot 提供的一个很实用的扩展点