Hi Marko
On 02/04/12 15:34, Voß, Marko wrote:
Hello,

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


The services page should return the list of all the REST (& WS) endpoints available and then individual WADL instances will describe all the root resources which constitute a given endpoint,

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>
Right now the model representing JAX-RS endpoints and the CXF model which is optimized for representing WS service and endpoints are not closely related, there's a link but it's not a 1 to 1 match, the following will only give you the list of all the root resource for a current endpoint.

Service service = serverFactoryBean.getServiceFactory().getService();
List<ClassResourceInfo> list = ((JAXRSServiceImpl)service).getClassResourceInfos();

They can also be retrieved at runtime from the current message:

Service service = message.getExchange().get(Service.class);
List<ClassResourceInfo> resources = ((JAXRSServiceImpl)service).getClassResourceInfos();

In order to get the list if all the jaxrs endpoints you'd need to get
a DestinationRegistryManager extension from the bus and enumerate over all the destinations and those which return null for getEndpointInfo().getInterface() do represent restful endpoints :-) so if possible try the services page for the latter task :-)

Sergey



Reply via email to