Find a list of all resource methods in my application?

Does Jersey provide any way to list all the resources it provides? That is, given the resource class:

package com.zoo.resource @Path("/animals") public class AnimalResource { @GET @Produces(MediaType.APPLICATION_JSON) @Path("dog") public Dog getDog(){ ... } @GET @Produces(MediaType.APPLICATION_JSON) @Path("cat") public Cat getCat(){ ... } } 

Does Jersey provide any way to get information:

  • GET on path /animals/dog returns type Dog
  • GET on path /animals/cat returns Cat type

(And besides, does it provide me with the opportunity to find out that AnimalResource is a resource?)

I would like this information to be available to me in the unit test, so that I can verify that every resource that I expose matches what the external system expects. I know that there is an automatic version that provides application.wadl , but I donโ€™t see it showing me return types, and I donโ€™t know how to access it from my tests.

+7
source share
1 answer

[update - the example is the same, but I reformulated my reservations]

It can be done. Try the following:

 import com.sun.jersey.api.model.AbstractResource; import com.sun.jersey.api.model.AbstractSubResourceMethod; import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; public class AnimalsTest { public static void main(String [] args) { AbstractResource resource = IntrospectionModeller.createResource(AnimalResource.class); System.out.println("Path is " + resource.getPath().getValue()); String uriPrefix = resource.getPath().getValue(); for (AbstractSubResourceMethod srm :resource.getSubResourceMethods()) { String uri = uriPrefix + "/" + srm.getPath().getValue(); System.out.println(srm.getHttpMethod() + " at the path " + uri + " return " + srm.getReturnType().getName()); } } } 

 class Dog {} 

 class Cat {} 

 @Path("/animals") class AnimalResource { @GET @Produces(MediaType.APPLICATION_JSON) @Path("dog") public Dog getDog(){ return null; } @GET @Produces(MediaType.APPLICATION_JSON) @Path("cat") public Cat getCat(){ return null; } } 

These introspection classes are in the jersey server.

Please note that in the above example, some Jersey classes are used that have an โ€œimplโ€ in the package name, which suggests that these Jersey classes are not intended for public consumption and can greatly disrupt changes in the future. I'm just thinking here - I'm not a Jersey supporter. Just a random user.

Also, everything is higher than I understood while looking at the source code. I have never seen documentation about the approved way to learn JAX-RS annotated classes. I agree that an officially supported API for this kind of operation will be very useful.

+7
source

All Articles