问题描述
让我们假设我有一个带嵌入式码头的简单球衣应用和下面的基本代码。
Let's suppose i have simple jersey app with embedded jetty Demo project on github and essential code below.
回到jersey1的日子我有日志信息:
Back 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'
但是现在我正在尝试使用jersey2而且thiere不再是日志信息。所以,我的问题是如何列出所有已注册的JAX-RS实体提供商。我不在乎在哪里列出它们。在 main
函数或某些 @GET
MyResource
方法中。可能我应该在我的设置中更改一些东西或者放置logger boolean,但是找不到它。
But now i am trying to work with jersey2 and thiere is no more log info. So, my question is how to list all registered JAX-RS Entity Providers. I don't care much where to list them. In main
function or in some @GET
MyResource
method. Probably i should change something in my setup or put logger boolean, but cant find it.
这纯粹是出于测试目的。所有提供者类的简单打印对我来说已经足够了。更酷的选择是打印提供商和已经合并的 @Produces
和 @Consumes
MediaType。
This is purely for testing purpose. Simple print of all providers classes will be enough for me. Cooler option is to print provider and accosiated @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();
}
}
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'
}
推荐答案
您可以使用,并且在 INITIAILZATION_FINISHED
上,您可以获得来自。例如
You can use an ApplicationEventListener
, and on INITIAILZATION_FINISHED
, you can get a set of resources and providers from the ApplicationEvent
. For example
@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;
}
}
另见:
- ,其中显示了遍历
ResourceModel
- Listing all deployed rest endpoints (spring-boot, jersey), which shows an example of traversing the
ResourceModel
这篇关于如何在泽西岛列出所有已注册的JAX-RS实体提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!