Spring Boot - List Endpoints on Startup

Listing all the endpoints on startup can help debugging and testing.

ApplicationListener

Create a bean that implements ApplicationListener<ApplicationReadyEvent>. This bean will be called when the application is ready. override the onApplicationEvent method to list all the endpoints.

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
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

@Component
@Slf4j
public class EndpointLogger implements ApplicationListener<ApplicationReadyEvent> {

private final RequestMappingHandlerMapping requestMappingHandlerMapping;

public EndpointLogger(RequestMappingHandlerMapping requestMappingHandlerMapping) {
this.requestMappingHandlerMapping = requestMappingHandlerMapping;
}

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
Map<RequestMappingInfo, ?> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
handlerMethods.forEach((key, value) -> {
log.info( key + " " + value);
});
}
}

output

1
2
3
{ [/error], produces [text/html]} org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
{ [/error]} org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
...

@EventListener annotation

Another way to listen to the ApplicationReadyEvent is to use the EventListener annotation. EventListener marks a method as a listener for application events.

1
2
3
4
5
6
7
8
9
@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("{} {}", key, value));
}

Actuator

Spring Boot Actuator provides a set of production-ready features to help you monitor and manage your application. It includes a set of built-in endpoints that can be used to monitor the application. To enable actuator, add the following dependency to the pom.xml file.

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

expose mappings endpoint or * to expose all endpoints

1
management.endpoints.web.exposure.include=mappings

visit http://localhost:8080/actuator/mappings to see all the mappings.