Thursday, November 10, 2011

PATCH methods on JAX-RS

We added PATCH semantics for Virgil.

This was fairly straight forward, except we need to add support for a @PATCH annotation and PatchMethod for HttpClient.

To do this, we created a PATCH annotation. Take a look at The contents of which are shown below:

public @interface PATCH {

This then allows us to use @PATCH on an annotation on a REST service.

@Produces({ "application/json" })
public void patchRow(@PathParam("keyspace") String keyspace,
@PathParam("columnFamily") String columnFamily, @PathParam("key") String key,
@QueryParam("index") boolean index, String body) throws Exception

That worked like a charm. Then we needed to call it using HttpClient. To that, we created a PatchMethod class that extended PostMethod. You can see that here.

Then we could use that just like any other HTTP method.

PatchMethod patch = new PatchMethod(BASE_URL + KEYSPACE + "/" + COLUMN_FAMILY + "/" + KEY);
requestEntity = new StringRequestEntity("{\"ADDR1\":\"1235 Fun St.\",\"COUNTY\":\"Montgomery\"}",
"appication/json", "UTF8");

Hope that helps people.


James said...

This is a minor thing, but for anyone hoping to follow the code in this post, you should consider extend putmethod not postmethod, since the semantics of the two mean that the postmethod could be handled differently (PUT, PATCH are idempotent, POST is not, so the client may call PUT any number of times until it gets a 'success' message, whereas POST is not).

stratis said...


"PATCH is neither safe nor idempotent as defined by [RFC2616], Section 9.1.
Collisions from multiple PATCH requests may be more dangerous than PUT collisions because some patch formats need to operate from a known base-point or else they will corrupt the resource. Clients using this kind of patch application SHOULD use a conditional request such that ..."

Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

Albert Smith said...

Wow, this is a comprehensive article on path methods and I have learned a lot of basic programming skills that will be of help to programming students looking for Secondary Data Research Proposal Writing Advice. Thanks so much for sharing this information and we are looking forward to reading more articles from this blog.

Mithun Mithun said...

Thanks for your informative articel .its very useful
dot net training center in chennai | dot net training institute in velachery | dot net training and placement in chennai