var*_*ren 5 java jax-rs jersey jersey-2.0
让我们假设我有一个简单的泽西应用程序,在github上嵌入了jetty Demo项目,下面是基本代码.
回到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'
Run Code Online (Sandbox Code Playgroud)
但现在我正在尝试使用jersey2和thiere不再是日志信息.所以,我的问题是如何列出所有已注册的JAX-RS实体提供商.我不在乎在哪里列出它们.在main功能或某种 @GET MyResource方法.可能我应该在我的设置中更改一些东西或者放置logger布尔值,但是找不到它.
这纯粹是为了测试目的.所有提供者类的简单打印对我来说已经足够了.冷却器选项是打印提供者和accosiated @Produces和@ConsumesMediaType.
MyResource.java
@Path("test")
public class MyResource {
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getPersons() {
return Response.ok("[some data here]").build();
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
和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'
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ApplicationEventListener,然后INITIALIZATION_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;
}
}
Run Code Online (Sandbox Code Playgroud)
也可以看看:
ResourceModel| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |