This example demonstrates how to implement generic support for HTTP PATCH Method (RFC-5789) via JAX-RS reader interceptor. The example has been created based on [this Gerard Davison's blogentry] (http://kingsfleet.blogspot.co.uk/2014/02/transparent-patch-support-in-jax-rs-20.html). The patch format supported by this example is JSON Patch (RFC-6902).
The mapping of the URI path space is presented in the following table:
| URI path | Resource class | HTTP methods |
|---|---|---|
| /patchable-state | PatchableResource | GET, PATCH |
As you can see in the table, there is only a single resource deployed in this application, that supports GET and PATCH methods. The resource represents a patchable state, which consists of 3 properties:
title text property,message text property, andlist property that represents a list/array of text strings.This state can be patch via HTTP GET method by sending the desired patch/diff in the JSON Patch format as defined in RFC-6902, for example:
[ { "op" : "replace", "path" : "/message", "value" : "patchedMessage" }, { "op" : "add", "path" : "/list/-", "value" : "one" } ]
This patch will instruct the resource to replace it's message property content with a new "patchedMessage" text and also to append a new "one" string value to the list of valued contained in the list property.
(See HttpPatchTest for more details.)
You can apply a patch using curl:
curl -v -X PATCH http://localhost:8080/http-patch/patchable-state -H "Content-Type:application/json-patch+json" -d '[{
"op": "replace",
"path": "/message",
"value": "patchedMessage"
}, {
"op": "add",
"path": "/list/-",
"value": "one"
}]'
The application will answer with a patched object:
{ "list" : [ "one" ], "message" : "patchedMessage", "title" : "" }
Run the example as follows:
mvn clean compile exec:java
This deploys the example using Grizzly container.
A WADL description may be accessed at the URL:
The resource is available at