public final class Resource extends Object implements Routed, ResourceModelComponent
Resource component model represents a collection of methods
grouped under the same parent request path template. Resource class is also
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.
For example:
@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() |
| Modifier and Type | Class and Description |
|---|---|
static class |
Resource.Builder
Resource model component builder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
accept(ResourceModelVisitor visitor)
A component should call the visitor back with an appropriate
visitor interface method to give it a chance to process.
|
static Resource.Builder |
builder()
Get a new unbound resource model builder.
|
static Resource.Builder |
builder(Class<?> resourceClass)
Create a resource model builder initialized by introspecting an annotated
JAX-RS resource class.
|
static Resource.Builder |
builder(List<Resource> resources)
Creates a
resource builder instance from the list of resource which can be merged
into a single resource. |
static Resource.Builder |
builder(Resource resource)
Get a new resource model builder initialized from a given resource model.
|
static Resource.Builder |
builder(String path)
Get a new resource model builder for a resource bound to a given path.
|
static Resource |
from(Class<?> resourceClass)
Create a resource model initialized by introspecting an annotated
JAX-RS resource class.
|
List<ResourceMethod> |
getAllMethods()
Provides resource methods and resource locator are available on the resource.
|
List<Resource> |
getChildResources()
Returns the list of child resources available on this resource.
|
List<? extends ResourceModelComponent> |
getComponents()
Should return all existing resource model sub-components.
|
Set<Class<?>> |
getHandlerClasses()
Get the method handler classes for the resource methods registered on the resource.
|
Set<Object> |
getHandlerInstances()
Get the method handler (singleton) instances for the resource methods registered
on the resource.
|
String |
getName()
Get the resource name.
|
String |
getPath()
Get the path direct assigned to the component.
|
static Path |
getPath(Class<?> resourceClass)
Get the resource class
@Path annotation. |
PathPattern |
getPathPattern()
Get the path pattern that can be used for matching the remaining
request URI against this component represented by this model.
|
ResourceMethod |
getResourceLocator()
Provides a resource locator available on the resource.
|
List<ResourceMethod> |
getResourceMethods()
Provides a non-null list of resource methods available on the resource.
|
static boolean |
isAcceptable(Class<?> c)
Check if the class is acceptable as a JAX-RS provider or resource.
|
String |
toString() |
public static Resource.Builder builder()
Resource.Builder.path(java.lang.String)public static Resource.Builder builder(String path)
path - resource path.Resource.Builder.path(java.lang.String)public static Resource.Builder builder(List<Resource> resources)
resource builder instance from the list of resource which can be merged
into a single resource. It must be possible to merge the resources into a single valid resource.
For example all resources must have the same path, they cannot have ambiguous methods
on the same path, etc.resources - Resources with the same path.public static Resource.Builder builder(Class<?> resourceClass) throws IllegalArgumentException
acceptability check,
on the resource class prior to the resource model creation.resourceClass - resource class to be modelled.null if the
class does not represent a resource.IllegalArgumentException - in case the class is not
acceptable
as a JAX-RS resource.public static Resource from(Class<?> resourceClass) throws IllegalArgumentException
acceptability check,
on the resource class prior to the resource model creation.resourceClass - resource class to be modelled.null if the
class does not represent a resource.IllegalArgumentException - in case the class is not
acceptable
as a JAX-RS resource.public static boolean isAcceptable(Class<?> c)
false if the class is either
c - class to be checked.true if the class is an acceptable JAX-RS provider or
resource, false otherwise.public static Path getPath(Class<?> resourceClass)
@Path annotation.
May return null in case there is no @Path annotation on the resource.resourceClass - resource class.@Path annotation instance if present on the resource class (i.e.
the class is a root resource class), or null otherwise.public static Resource.Builder builder(Resource resource)
resource - resource model initializing the resource builder.public String getPath()
Routedpublic PathPattern getPathPattern()
RoutedgetPathPattern in interface Routedpublic String getName()
fully-qualified name
of the resource class.public List<ResourceMethod> getResourceMethods()
public ResourceMethod getResourceLocator()
public List<ResourceMethod> getAllMethods()
public List<Resource> getChildResources()
public Set<Class<?>> getHandlerClasses()
public Set<Object> getHandlerInstances()
public void accept(ResourceModelVisitor visitor)
ResourceModelComponentaccept in interface ResourceModelComponentvisitor - resource model visitor.public List<? extends ResourceModelComponent> getComponents()
ResourceModelComponentgetComponents in interface ResourceModelComponentCopyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.