简体   繁体   English

JAX-RS Jersey,如何动态地向Application添加资源或提供者

[英]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;
  } 
}

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. 我想知道如何动态注入我的资源以在运行时设置,我的Web应用程序将在运行时创建几个新资源,并需要注入到Application以便使用。

Programmatic API for building resources 用于构建资源的编程API

The Resource class is what your are looking for. Resource类正是您正在寻找的。 Just mind it's a Jersey specific API. 请记住它是泽西特有的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. 根据文档, Resource类是编程资源建模API的主要入口点,它提供了以编程方式扩展现有JAX-RS带注释资源类或构建Jersey运行时可能使用的新资源模型的能力。

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 . 有关其他详细信息,请查看Jersey文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM