Listing All Registered JAX-RS Service Providers in Jersey

Suppose I have a simple jersey app with a built -in github demo app and important code below.

In the days with jersey1, I had log messages:

 07, 2016 5:05:50 AM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Scanning for root resource and provider classes in the packages: ru.varren  07, 2016 5:05:50 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Root resource classes found: class ru.varren.MyResource  07, 2016 5:05:50 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Provider classes found: class ru.varren.JsonProvider  07, 2016 5:05:50 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate INFO: Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016 02:08 PM' 

But now I'm trying to work with jersey2, and thiere is no longer magazine information. So my question is how to list all the registered JAX-RS Entity Providers . I don’t care where to list them. In the main function or in some @GET method. Probably, I should change something in my setup or set the login to logical, but I can not find it.

This is purely for testing purposes. A simple listing of all classes of providers will be enough for me. "Cooler" is designed for printing and matching @Produces and @Consumes MediaType.

Myresource.java

 @Path("test") public class MyResource { @GET @Produces({ MediaType.APPLICATION_JSON }) public Response getPersons() { return Response.ok("[some data here]").build(); } } 

Myapplication.java

 public class MyApplication { public static void main(String[] args) throws Exception { URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build(); ResourceConfig config = new ResourceConfig(MyResource.class); config.packages("ru.varren"); Server server = JettyHttpContainerFactory.createServer(baseUri, config, true); server.join(); } } 

And gradle.build

 dependencies { compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.22.2' compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.22.2' compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.22.2' compile 'org.glassfish.jersey.containers:jersey-container-jetty-http:2.22.2' compile 'org.eclipse.jetty:jetty-server:9.1.0.M0' compile 'org.eclipse.jetty:jetty-servlet:9.1.0.M0' } 
+5
source share
1 answer

You can use ApplicationEventListener , and at INITIAILZATION_FINISHED you can get a set of resources and providers from ApplicationEvent . for instance

 @Provider public class ProviderLoggingListener implements ApplicationEventListener { @Override public void onEvent(ApplicationEvent event) { switch (event.getType()) { case INITIALIZATION_FINISHED: { Set<Class<?>> providers = event.getProviders(); ResourceConfig immutableConfig = event.getResourceConfig(); ResourceModel resourcesModel = event.getResourceModel(); break; } } } @Override public RequestEventListener onRequest(RequestEvent requestEvent) { return null; } } 

See also:

+3
source

All Articles