用于构建资源的编程 API
资源
类是您要查找的内容。请注意,这是一个泽西岛特定的API。
根据文档,Resource
类是编程资源建模 API 的主要入口点,它提供了以编程方式扩展现有 JAX-RS 注释资源类或构建 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()
有关其他详细信息,请查看泽西岛文档。