JAX-RS Jersey,如何向应用程序动态添加资源或提供程序 [英] JAX-RS Jersey, how to dynamic adding resources or providers to Application
问题描述
public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<>();
private Set<Class<?>> classes = new HashSet<>();
public ShoppingApplication() {
classes.add(CustomerResourceService.class);
classes.add(JAXBMarshaller.class);
classes.add(JSONMarshaller.class);
singletons.add(new CustomerResourceService());
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
假设我有上面的代码,该代码扩展了Application并注册了要设置的资源或提供程序.我想知道如何动态地在运行时注入资源以进行设置,我的Web应用程序将在运行时创建多个新资源,并且需要注入到应用程序中才能使用.
用于构建资源的程序化API
Resource
类是您想要的.只需注意这是针对Jersey的API.
根据文档,
下表说明了上例中配置的应用程序支持的请求和提供的响应: 有关其他详细信息,请参阅 Jersey文档.@Path("hello")
public class HelloResource {
@GET
@Produces("text/plain")
public String sayHello() {
return "Hello!";
}
}
// Register the annotated resource.
ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class);
// Add new "hello2" resource using the annotated resource class
// and overriding the resource path.
Resource.Builder resourceBuilder =
Resource.builder(HelloResource.class, new LinkedList<ResourceModelIssue>())
.path("hello2");
// Add a new (virtual) sub-resource method to the "hello2" resource.
resourceBuilder.addChildResource("world")
.addMethod("GET")
.produces("text/plain")
.handledBy(new Inflector<Request, String>() {
@Override
public String apply(Request request) {
return "Hello World!";
}
});
// Register the new programmatic resource in the application's configuration.
resourceConfig.registerResources(resourceBuilder.build());
Request | Response | Method invoked
-----------------------+------------------+----------------------------
GET /hello | "Hello!" | HelloResource.sayHello()
GET /hello2 | "Hello!" | HelloResource.sayHello()
GET /hello2/world | "Hello World!" | Inflector.apply()
public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<>();
private Set<Class<?>> classes = new HashSet<>();
public ShoppingApplication() {
classes.add(CustomerResourceService.class);
classes.add(JAXBMarshaller.class);
classes.add(JSONMarshaller.class);
singletons.add(new CustomerResourceService());
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
Suppose I have above code which I extends Application and register my resources or providers to set. I want to know how can I dynamically inject my resources to set in runtime, my web application will create several new resources in runtime and need to inject to Application inorder to use.
Programmatic API for building resources
The Resource
class is what your are looking for. Just mind it's a Jersey specific API.
According to the documentation, the Resource
class is the main entry point to the programmatic resource modeling API that provides ability to programmatically extend the existing JAX-RS annotated resource classes or build new resource models that may be utilized by Jersey runtime.
Have a look at the example provided by the documentation:
@Path("hello")
public class HelloResource {
@GET
@Produces("text/plain")
public String sayHello() {
return "Hello!";
}
}
// Register the annotated resource.
ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class);
// Add new "hello2" resource using the annotated resource class
// and overriding the resource path.
Resource.Builder resourceBuilder =
Resource.builder(HelloResource.class, new LinkedList<ResourceModelIssue>())
.path("hello2");
// Add a new (virtual) sub-resource method to the "hello2" resource.
resourceBuilder.addChildResource("world")
.addMethod("GET")
.produces("text/plain")
.handledBy(new Inflector<Request, String>() {
@Override
public String apply(Request request) {
return "Hello World!";
}
});
// Register the new programmatic resource in the application's configuration.
resourceConfig.registerResources(resourceBuilder.build());
The following table illustrates the supported requests and provided responses for the application configured in the example above:
Request | Response | Method invoked
-----------------------+------------------+----------------------------
GET /hello | "Hello!" | HelloResource.sayHello()
GET /hello2 | "Hello!" | HelloResource.sayHello()
GET /hello2/world | "Hello World!" | Inflector.apply()
For additional details, check the Jersey documentation.
这篇关于JAX-RS Jersey,如何向应用程序动态添加资源或提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!