Is it possible to list all my configured rest-endpoints with spring boot? The actuator lists all existing paths on startup, I want something similar for my custom services, so I can check on startup if all paths are configured correctly and use this info for client calls.
How do I do this? I use @Path
/@GET
annotations on my service beans and register them via ResourceConfig#registerClasses
.
Is there a way to query the Config for all Paths?
Update: I register the REST Controllers via
@Bean
public ResourceConfig resourceConfig() {
return new ResourceConfig() {
{
register(MyRestController.class);
}
};
}
Update2: I want to have something like
GET /rest/mycontroller/info
POST /res/mycontroller/update
...
Motivation: when the spring-boot app started, I want to print out all registered controllers and their paths, so I can stop guessing which endpoints to use.
What about using
RequestMappingHandlerMapping
that hold all endpoints information.See my answer at How to access all available routes of a REST API from a controller?.
Can you use
ResourceConfig#getResources
on yourResourceConfig
object then get the info you need by iterating through theSet<Resource>
it returns?Apologies, would try it, but I don't have the Resources to do it right now. :-p
Probably the best way to do this, is to use an
ApplicationEventListener
. From there you can listen for the "application finished initializing" event, and get theResourceModel
from theApplicationEvent
. TheResourceModel
will have all the initializedResource
s. Then you can traverse theResource
as others have mentioned. Below is an implementation. Some of the implementation has been taken from theDropwizardResourceConfig
implementation.Then you just need to register the listener with Jersey. You can get the application path from the
JerseyProperties
. You will need to have set it in the Spring Bootapplication.properties
under the propertyspring.jersey.applicationPath
. This will be the root path, just as if you were to use@ApplicationPath
on yourResourceConfig
subclassOne thing to note, is that the load-on-startup is not set by default on the Jersey servlet. What this means is that that Jersey won't load on startup until the first request. So you will not see the listener triggered until the first request. I have opened an issue to possible get a configuration property, but in the meantime, you have a couple options:
Set up Jersey as filter, instead of a servlet. The filter will be loaded on start up. Using Jersey as a filter, for the most post, really doesn't behave any differently. To configure this you just need to add a Spring Boot property in the
application.properties
The other option is to override the Jersey
ServletRegistrationBean
and set itsloadOnStartup
property. Here is an example configuration. Some of the implementation has been taken straight from theJerseyAutoConfiguration
UPDATE
So it looks like Spring Boot is going to add the
load-on-startup
property, so we don't have to override the JerseyServletRegistrationBean
. Will be added in Boot 1.4.0After the application is fully started, you can ask
ServerConfig
:classes
contains all the cached endpoint classes.From the API docs for
javax.ws.rs.core.Configuration
:However, you can't do this in the init code of your application, the classes might not yet be fully loaded.
With the classes, you can scan them for the resources: