Resource methods are annotated using JAX-RS annotations. They have one of the HTTP method annotation specifying the types of requests that the method processes. JAX-RS places several constraints on resource methods.
All resource methods must meet the following conditions:
It must be public.
It must be decorated with one of the HTTP method annotations described in Specifying HTTP verbs.
It must not have more than one entity parameter as described in Parameters.
Resource method parameters take two forms:
entity parameters—Entity parameters are not annotated. Their value is mapped from the request entity body. An entity parameter can be of any type for which your application has an entity provider. Typically they are JAXB objects.
Important A resource method can have only one entity parameter.
For more information on entity providers see Entity Support.
annotated parameters—Annotated parameters use one of the JAX-RS annotations that specify how the value of the parameter is mapped from the request. Typically, the value of the parameter is mapped from portions of the request URI.
For more information about using the JAX-RS annotations for mapping request data to method parameters see Passing Information into Resource Classes and Methods.
Example 2.4 shows a resource method with a valid parameter list.
Example 2.4. Resource method with a valid parameter list
@POST @Path("disaster/monster/giant/{id}") public void addDaikaiju(Kaiju kaiju, @PathParam("id") String id) { ... }
Example 2.5 shows a resource method with an invalid parameter list. It has two parameters that are not annotated.
Example 2.5. Resource method with an invalid parameter list
@POST @Path("disaster/monster/giant/") public void addDaikaiju(Kaiju kaiju, String id) { ... }
Resource methods can return one of the following:
void
any Java class for which the application has an entity provider
For more information on entity providers see Entity Support.
a
Response
objectFor more information on
Response
objects see Fine tuning an application's responses.a
GenericEntity<
objectT
>For more information on
GenericEntity<
objects see Returning entities with generic type information.T
>
All resource methods return an HTTP status code to the requester. When the return type of the method is
void
or the value being returned is null
, the resource method sets the HTTP status
code to 200
. When the resource method returns any value other than null
,
it sets the HTTP status code to 204
.