Hello,

I need to know the existing endpoints of a JAX-RS interface.

I did the following solution first:

List<ClassResourceInfo> resources = 
jaxrsServerFactoryBean.getServiceFactory().getClassResourceInfo();
for (ClassResourceInfo resource : resources) {
     if (FooRestService.class.isAssignableFrom(resource.getServiceClass())) {
         LOG.warn("Foo REST-Service locally available!");
         LOG.warn("Service path: " + resource.getPath().value());
         for (OperationResourceInfo ori : 
resource.getMethodDispatcher().getOperationResourceInfos()) {
             if (ori.getAnnotatedMethod().getName().equals("retrieve")) {
                 LOG.warn("Method path: " + 
ori.getAnnotatedMethod().getAnnotation(Path.class).value());
             }
         }
     }
}

Output:

Foo REST-Service locally available!
Service path: /foo/something
Method path: /{id}


This solution however may not work, when dealing with distributed services. One 
server instance may not have all 
services registered to its jaxrs:server bean configured in Spring.

What we do have available on all instances are the JAX-RS interfaces because of 
we need to perform requests to the other 
instances of course.

So, is there an easy way to get all the endpoints or do I have to implement the 
reflection on my own?

I do know, that CXF is already handling this in some classes. I tried the 
following, which does not work, because the 
iteration has no items to iterate through. I would prefer to reuse the existing 
code in CXF for this.

JAXRSServiceFactoryBean serviceFactoryBean = new JAXRSServiceFactoryBean();
serviceFactoryBean.setResourceClass(FooRestServiceImpl.class);
Service service = serviceFactoryBean.create();
for (Endpoint endpoint : service.getEndpoints().values()) {
     LOG.warn("Endpoints: " + endpoint.getEndpointInfo());
}

Output:

<nothing>



Thank you in advance and best regards,

Marko


-------------------------------------------------------

Fachinformationszentrum Karlsruhe, Gesellschaft für wissenschaftlich-technische 
Information mbH. 
Sitz der Gesellschaft: Eggenstein-Leopoldshafen, Amtsgericht Mannheim HRB 
101892. 
Geschäftsführerin: Sabine Brünger-Weilandt. 
Vorsitzender des Aufsichtsrats: MinDirig Dr. Thomas Greiner.


Reply via email to