簡體   English   中英

如何列出澤西島所有已注冊的 JAX-RS 實體提供者

[英]How to list all registered JAX-RS Entity Providers in Jersey

假設我有一個簡單的球衣應用程序, 在 github 上有嵌入式碼頭演示項目和下面的基本代碼。

回到 jersey1 的日子里,我有日志消息:

мая 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 並且沒有更多的日志信息。 所以,我的問題是如何列出所有已注冊的 JAX-RS Entity Providers 我不太關心在哪里列出它們。 main函數或某些@GET MyResource方法中。 可能我應該在我的設置中改變一些東西或者把記錄器布爾值,但找不到它。

這純粹是為了測試目的。 對我來說,所有提供者類的簡單打印就足夠了。 Cooler 選項是打印提供者和關聯的@Produces@Consumes MediaType。

我的資源

@Path("test")
public class MyResource {

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

}

我的應用程序

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'
}

您可以使用ApplicationEventListener ,並且在INITIALIZATION_FINISHED ,您可以從ApplicationEvent獲取一組資源和提供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;
    }
}

另見:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM