[英]How to list all registered JAX-RS Entity Providers in Jersey
Let's suppose i have simple jersey app with embedded jetty Demo project on github and essential code below.假设我有一个简单的球衣应用程序, 在 github 上有嵌入式码头演示项目和下面的基本代码。
Back in the days with jersey1 i had log messages:回到 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'
But now i am trying to work with jersey2 and thiere is no more log info.但是现在我正在尝试使用 jersey2 并且没有更多的日志信息。 So, my question is how to list all registered JAX-RS Entity Providers .
所以,我的问题是如何列出所有已注册的 JAX-RS Entity Providers 。 I don't care much where to list them.
我不太关心在哪里列出它们。 In
main
function or in some @GET
MyResource
method.在
main
函数或某些@GET
MyResource
方法中。 Probably i should change something in my setup or put logger boolean, but cant find it.可能我应该在我的设置中改变一些东西或者把记录器布尔值,但找不到它。
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. Cooler 选项是打印提供者和关联的
@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();
}
}
And gradle.build和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'
}
You can use an ApplicationEventListener
, and on INITIALIZATION_FINISHED
, you can get a set of resources and providers from the ApplicationEvent
.您可以使用
ApplicationEventListener
,并且在INITIALIZATION_FINISHED
,您可以从ApplicationEvent
获取一组资源和提供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;
}
}
See Also:另见:
ResourceModel
ResourceModel
的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.