13

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.

Jack Sierkstra
  • 1,414
  • 2
  • 20
  • 42
  • http://stackoverflow.com/questions/28993724/is-there-a-way-to-discover-all-endpoints-of-a-rest-api – James Jithin Nov 06 '15 at 14:54
  • Sorry, but this doesn't remotely answer my question. My question is targeted at Spring Framework. I have access to my own code, so I won't brute force my own application. Furthermore, you can expect such functionality of a relatively mature framework as Spring. The endpoints are somewhere registered (Spring Container??), so there must be a way to iterate through them. – Jack Sierkstra Nov 06 '15 at 17:29
  • 1
    http://stackoverflow.com/questions/11082818/spring-mvc-get-all-request-mappings – James Jithin Nov 06 '15 at 19:01
  • Now that is way more close. In the meantime I got some advice to use Spring Boot Actuator. I do not have that possibility, so I went on looking in the source of Spring Boot Actuator and saw this file: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/RequestMappingEndpoint.java. I'm going test that in my application. Thanks for the advice! I will edit my post once I have an answer ready. – Jack Sierkstra Nov 06 '15 at 19:12
  • Cool! All the best Jack! – James Jithin Nov 06 '15 at 19:27
  • 1
    @JackSierkstra If you get the answer you can post it as an answer and accept it. – Kannan Ramamoorthy Jun 17 '17 at 16:48
  • can you check the link below. https://stackoverflow.com/questions/32525699/listing-all-deployed-rest-endpoints-spring-boot-jersey/35237151#35237151 – Cahit Yiğit Çetiner Jul 29 '19 at 10:45
  • Check out this thread: https://stackoverflow.com/questions/43541080/how-to-get-all-endpoints-list-after-startup-spring-boot/59165297 – Thomas Verhoeven Jan 20 '21 at 09:59
  • This might come in handy `git grep -hi "@.*Mapping"` – Hritik Dec 15 '22 at 08:17

3 Answers3

1

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
Kamran Ullah
  • 101
  • 9
0

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));
}
-1

I assume based on how the questions is worded that you are not using Spring Boot, if you were, the actuator mappings endpoint does this for you, but your answer lies in how the mappings endpoint is build in actuator. There is a RequestMappingHandlerMapping object you leverage.

fpmoles
  • 1,209
  • 8
  • 14