Skip to main content

PATCH methods on JAX-RS

Posted by boneill42 on November 10, 2011 at 12:49 PM PST

 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 PATCH.java. The contents of which are shown below:
[prettifystyle="color: rgb(0, 0, 0); line-height: normal; text-align: left; "]@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME)<br />@HttpMethod(&quot;PATCH&quot;)<br />public @interface PATCH {<br />} [/prettify]

This then allows us to use @PATCH on an annotation on a REST service.
[prettifystyle="color: rgb(0, 0, 0); line-height: normal; text-align: left; "]@PATCH<br />@Path(&quot;/data/{keyspace}/{columnFamily}/{key}&quot;)<br />@Produces({ &quot;application/json&quot; })<br />public void patchRow(@PathParam(&quot;keyspace&quot;) String keyspace,<br />@PathParam(&quot;columnFamily&quot;) String columnFamily, @PathParam(&quot;key&quot;) String key,<br />@QueryParam(&quot;index&quot;) boolean index, String body) throws Exception [/prettify]

That worked like a charm. Then we needed to call it using HttpClient. To that, we created a PatchMethod class that extended PostMethod.  See below:

public class PatchMethod extends PostMethod {

        public PatchMethod(String url){
                super(url);
        }
        @Override
        public String getName() {
                return "PATCH";
        }
}
Then we could use that just like any other HTTP method within HTTP client.
[prettifystyle="color: rgb(0, 0, 0); line-height: normal; text-align: left; "]PatchMethod patch = new PatchMethod(BASE_URL + KEYSPACE + &quot;/&quot; + COLUMN_FAMILY + &quot;/&quot; + KEY);<br />requestEntity = new StringRequestEntity(&quot;{\&quot;ADDR1\&quot;:\&quot;1235 Fun St.\&quot;,\&quot;COUNTY\&quot;:\&quot;Montgomery\&quot;}&quot;,<br />&quot;appication/json&quot;, &quot;UTF8&quot;);<br />patch.setRequestEntity(requestEntity); [/prettify]

 

Related Topics >>