Java Reference
In-Depth Information
The Server Code
Using PATCH within JAX-RS is very simple. The source code under the
ex04_1
directory
contains a simple annotation that implements PATCH:
src/main/java/org/ieft/annotations/PATCH.java
package
package
org
.
ieft
.
annotations
;
import
import
javax.ws.rs.HttpMethod
javax.ws.rs.HttpMethod
;
import
import
java.lang.annotation.*
java.lang.annotation.*
;
@Target
({
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@HttpMethod
(
"PATCH"
)
public
public
@interface
PATCH
{
}
As described in
Chapter 4
, all you need to do to use a custom HTTP method is annotate an
annotation class with
@javax.ws.rs.HttpMethod
. This
@HttpMethod
declaration must con-
tain the value of the new HTTP method you are defining.
To illustrate the use of our new
@PATCH
annotation, I expanded a little bit on the example
code discussed in
Chapter 18
.
A simple JAX-RS method is added to the
CustomerResource
class that can handle PATCH requests:
src/main/java/com/restfully/shop/services/CustomerResource.java
package
package
com
.
restfully
.
shop
.
services
;
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
...
@PATCH
@Path
(
"{id}"
)
@Consumes
(
"application/xml"
)
public
public
void
void
patchCustomer
(
@PathParam
(
"id"
)
int
int
id
,
InputStream is
)
{
updateCustomer
(
id
,
is
);
}
...
}