2016-05-07 31 views
5

假設我有一個簡單的澤西島應用程序,內嵌碼頭Demo project on github和下面的基本代碼。早在與jersey1天如何列出澤西島內所有註冊的JAX-RS實體提供商

我有日誌消息:

мая 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' 

但現在我試圖用jersey2工作,thiere沒有更多的日誌信息。所以,我的問題是如何列出所有註冊的JAX-RS實體提供商。我不在乎在哪裏列出它們。在main功能或在一些@GETMyResource方法。也許我應該改變我的設置或把記錄器布爾值,但無法找到它。

這純粹是爲了測試目的。所有提供者類的簡單打印對我來說就足夠了。散熱器選項是打印供應商並敲打@Produces@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(); 
    } 

} 

而且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' 
} 
+0

我可能會嘗試使用['ApplicationEventListener'](HTTPS的示例:// jersey.java.net/apidocs/2.22.2/jersey/org/glassfish/jersey/server/monitoring/ApplicationEvent.html)和「初始化應用程序」使用['ApplicationEvent']上的方法(https:// jersey.java.net/apidocs/2 .22.2/jersey/org/glassfish/jersey/server/monitoring/ApplicationEvent.html)來獲取資源和提供者 –

+0

@peeskillet yep,exectly我在找什麼。在自定義的'ApplicationEventListener'類的'onEvent'方法中'applicationEvent.getProviders()'給了我所需要的所有信息。 https://i.gyazo.com/7050dccb82d4c2445709b2079817ce00.png這裏也是很酷的文檔https://jersey.java.net/documentation/latest/monitoring_tracing.html。你能否發表你的評論作爲答案?我會將其標記爲已接受的答案 – varren

+0

@peeskillet是否有任何與供應商無關的API? –

回答

3

你可以使用ApplicationEventListenerINITIAILZATION_FINISHED,您可以從ApplicationEvent獲得一組資源和提供者。例如

@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; 
    } 
} 

另請參見: