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,
After the application is fully started, you can ask ServerConfig:
ResourceConfig instance;
ServerConfig scfg = instance.getConfiguration();
Set> classes = scfg.getClasses();
classes contains all the cached endpoint classes.
From the API docs for javax.ws.rs.core.Configuration:
Get the immutable set of registered JAX-RS component (such as provider or feature) classes to be instantiated, injected and utilized in the scope of the configurable instance.
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:
public Map> scan(Class baseClass) {
Builder builder = Resource.builder(baseClass);
if (null == builder)
return null;
Resource resource = builder.build();
String uriPrefix = "";
Map> info = new TreeMap<>();
return process(uriPrefix, resource, info);
}
private Map> process(String uriPrefix, Resource resource, Map> info) {
String pathPrefix = uriPrefix;
List resources = new ArrayList<>();
resources.addAll(resource.getChildResources());
if (resource.getPath() != null) {
pathPrefix = pathPrefix + resource.getPath();
}
for (ResourceMethod method : resource.getAllMethods()) {
if (method.getType().equals(ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR)) {
resources.add(
Resource.from(
resource.getResourceLocator()
.getInvocable()
.getDefinitionMethod()
.getReturnType()
)
);
}
else {
List paths = info.get(pathPrefix);
if (null == paths) {
paths = new ArrayList<>();
info.put(pathPrefix, paths);
}
InfoLine line = new InfoLine();
line.pathPrefix = pathPrefix;
line.httpMethod = method.getHttpMethod();
paths.add(line);
System.out.println(method.getHttpMethod() + "\t" + pathPrefix);
}
}
for (Resource childResource : resources) {
process(pathPrefix, childResource, info);
}
return info;
}
private class InfoLine {
public String pathPrefix;
public String httpMethod;
}