[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.
Guido simone
source share