|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.glassfish.jersey.server.model.Resource
public final class Resource
Model of a single resource component.
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.addMethod("GET")
.path("world")
.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.addResources(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() |
| Nested Class Summary | |
|---|---|
static class |
Resource.Builder
Resource model component builder. |
| Method Summary | |
|---|---|
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(java.lang.Class<?> resourceClass,
java.util.List<ResourceModelIssue> issueList)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource class. |
static Resource.Builder |
builder(java.lang.Object resource,
java.util.List<ResourceModelIssue> issueList)
Create a resource model builder initialized by introspecting an annotated JAX-RS resource instance. |
static Resource.Builder |
builder(Resource resource)
Get a new resource model builder initialized from a given resource model. |
static Resource.Builder |
builder(java.lang.String path)
Get a new resource model builder for a resource bound to a given path. |
java.util.List<? extends ResourceModelComponent> |
getComponents()
Should return all existing resource model sub-components. |
java.lang.String |
getName()
Get the resource name. |
java.lang.String |
getPath()
Get the path direct assigned to the component. |
static Path |
getPath(java.lang.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. |
java.util.List<ResourceMethod> |
getResourceMethods()
Provides a non-null list of resource methods available on the resource. |
java.util.List<ResourceMethod> |
getSubResourceLocators()
Provides a non-null list of sub-resource locators available on the resource. |
java.util.List<ResourceMethod> |
getSubResourceMethods()
Provides a non-null list of sub-resource methods available on the resource. |
static boolean |
isAcceptable(java.lang.Class<?> c)
Check if the class is acceptable as a JAX-RS provider or resource. |
boolean |
isRootResource()
Check if this resource model models a JAX-RS root resource. |
java.lang.String |
toString()
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Method Detail |
|---|
public static Resource.Builder builder()
root resource.
This can be changed by invoking the path(...) method on the returned builder.
Resource.Builder.path(java.lang.String)public static Resource.Builder builder(java.lang.String path)
root resource.
path - resource path.
Resource.Builder.path(java.lang.String)
public static Resource.Builder builder(java.lang.Class<?> resourceClass,
java.util.List<ResourceModelIssue> issueList)
throws java.lang.IllegalArgumentException
acceptability check,
on the resource class prior to the resource model creation.
resourceClass - resource class to be modelled.issueList - mutable list of issues that will be updated with the
introspection-specific issues found in the model.
java.lang.IllegalArgumentException - in case the class is not
acceptable as a JAX-RS resource.
public static Resource.Builder builder(java.lang.Object resource,
java.util.List<ResourceModelIssue> issueList)
builder(Class, java.util.List), this method does not perform
the acceptability check, since it is
assumed that the instance of the resource has already been created and is
acceptable.
resource - resource instance to be modelled.issueList - mutable list of issues that will be updated with the
introspection-specific issues found in the model.
public static boolean isAcceptable(java.lang.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(java.lang.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 boolean isRootResource()
true, if this is a model of a JAX-RS root resource,
false otherwise.public java.lang.String getPath()
Routed
getPath in interface Routedpublic PathPattern getPathPattern()
Routed
getPathPattern in interface Routedpublic java.lang.String getName()
fully-qualified name
of the resource class.
public java.util.List<ResourceMethod> getResourceMethods()
public java.util.List<ResourceMethod> getSubResourceMethods()
public java.util.List<ResourceMethod> getSubResourceLocators()
public void accept(ResourceModelVisitor visitor)
ResourceModelComponent
accept in interface ResourceModelComponentvisitor - resource model visitor.public java.lang.String toString()
toString in class java.lang.Objectpublic java.util.List<? extends ResourceModelComponent> getComponents()
ResourceModelComponent
getComponents in interface ResourceModelComponent
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||