Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output all endpoints exposed by Spring

Tags:

java

spring

I'd like to have a way how to expose all endpoints that exposed by my Spring application. Is there a simple way to check, for each @profile which are exposed?

Example:

GET   /api/resource
GET   /api/resource/list
POST  /api/resource
PUT   /api/resource

In the past, I have used a web application made in Laravel, and they had a simple cli method for checking the exposed methods.

like image 934
Jack Sierkstra Avatar asked Jan 19 '26 15:01

Jack Sierkstra


2 Answers

The best solution is to use Spring boot actuator and hit the endpoint /actuator/mappings to get all the endpoints.

But if you can't use actuator or can't add it as dependency you can retrieve all the endpoints programmatically the mapping handlers, Spring get shipped with three implementations of this interface (HandlerMapping):

  • RequestMappingHandlerMapping: which is responsible for endpoints that annotated with @RequestMapping and its variants @GetMapping, @PostMapping .. etc

  • BeanNameUrlHandlerMapping: as the name suggest it will resolve the endpoint(URL) directly to a bean in the application context. for example if you hit the endpoint /resource it will look for a bean with the name /resource.

  • RouterFunctionMapping: it will scan the application context for RouterFunction beans and dispatch the request to that function.

Anyways, to answer your question you can autowire the bean RequestMappingHandlerMapping and print out all the handler methods. Something similar to this:

@Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;

@PostConstruct
public void printEnpoints() {
    requestMappingHandlerMapping.getHandlerMethods().forEach((k,v) -> System.out.println(k + " : "+ v));
}
like image 104
Salem AlHarbi Avatar answered Jan 21 '26 08:01

Salem AlHarbi


In this scenario you can use two approaches:

  1. Spring Boot Actuator feature. Your endpoints of application will be available at http://host/actuator/mappings
  2. Swagger library can also be used to list all endpoints of a REST API
like image 36
Kamran Ullah Avatar answered Jan 21 '26 08:01

Kamran Ullah