查看spring boot api 接口

88 阅读1分钟

Listener方式

ApiApplicationListener

@Slf4j
public class ApiApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
                .getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        map.forEach((key, value) -> log.info("api listener, key: {}, value: {}", key, value));
    }
}

需要增加spring.factories配置文件

resources/META-INF/spring.factories

# Application Listeners
org.springframework.context.ApplicationListener=\
org.example.listener.ApiApplicationListener

注解方式

@EventListener

@Slf4j
@Configuration
public class SpringConfiguration implements WebMvcConfigurer {

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
                .getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        map.forEach((key, value) -> log.info("api, key: {}, value: {}", key, value));
    }
}

actuator


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

appplication.yaml

management:
  endpoints:
    web:
      exposure:
        include: "mappings"

通过浏览器访问:http://localhost:8080/actuator/mappings

image.png