programing

기동 후 스프링 부트 후 모든 엔드포인트 목록을 가져오는 방법

cafebook 2023. 3. 14. 21:53
반응형

기동 후 스프링 부트 후 모든 엔드포인트 목록을 가져오는 방법

스프링 부츠로 작성한 휴식 서비스가 있습니다.기동 후에 모든 엔드포인트를 취득하고 싶다.어떻게 하면 될까요?이를 위해 시작 후 모든 엔드포인트를 DB에 저장하고(아직 존재하지 않는 경우) 이를 인증에 사용합니다.이러한 엔트리는 역할에 삽입되고 역할은 토큰 작성에 사용됩니다.

Request Mapping Handler Mapping은 응용 프로그램콘텍스트의 선두에 표시됩니다.

@Component
public class EndpointsListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods()
             .forEach(/*Write your code here */);
    }
}

또는 모든 엔드포인트를 json으로 나열하는 다른 엔드포인트(매핑엔드포인트)를 표시하는 Spring boot actuator(스프링 부트를 사용하지 않는 경우에도 액튜테이터를 사용할 수 있습니다).이 엔드포인트를 히트하고 json을 구문 분석하여 엔드포인트 목록을 가져올 수 있습니다.

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-endpoints

모든 엔드포인트를 표시하려면 다음 3단계를 수행해야 합니다.

  1. 스프링 부트 액추에이터 활성화
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 엔드포인트 사용

Spring Boot 2에서는 대부분의 엔드포인트가 비활성화되어 있습니다.기본적으로 사용할 수 있는 것은 다음 2개뿐입니다.

/health
/info

모든 엔드포인트를 활성화하려면 다음과 같이 설정합니다.

management.endpoints.web.exposure.include=*

상세한 것에 대하여는, 다음을 참조해 주세요.

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

  1. 가!

http://host/module/module

btw, Spring Boot 2에서 Actuator는 보안 모델을 애플리케이션 모델과 병합하여 보안 모델을 단순화합니다.

상세한 것에 대하여는, 다음의 문서를 참조해 주세요.

https://www.baeldung.com/spring-boot-actuators

상기의 코멘트 에, Spring 4.2 이후,@EventListener주석:

@Component
public class EndpointsListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(EndpointsListener.class);

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        applicationContext.getBean(RequestMappingHandlerMapping.class)
            .getHandlerMethods().forEach((key, value) -> LOGGER.info("{} {}", key, value));
    }
}

Spring 이벤트의 사용 방법과 커스텀 이벤트의 작성 방법에 대해서는, 다음의 기사를 참조해 주세요.Spring Events

application.properties에는 management.endpoints가 필요합니다.web.disc.disc = discloss

다음으로 모든 엔드포인트를 http://localhost:8080/actuator/mappings에서 확인할 수 있습니다.

POM에 액튜에이터를 추가하는 것을 잊지 마세요.

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

파티에는 늦었지만 직접 이용하실 수 있습니다.

@Autowired
private RequestMappingHandlerMapping requestHandlerMapping;

this.requestHandlerMapping.getHandlerMethods()
            .forEach((key, value) ->  /* whatever */));

언급URL : https://stackoverflow.com/questions/43541080/how-to-get-all-endpoints-list-after-startup-spring-boot

반응형