Search code examples
javajax-rs

How to have a @PATCH annotation for JAX-RS?


JAX-RS has annotations for HTTP verbs such as GET (@GET) and POST (@POST) but there is no @PATCH annotation. How can I have an annotation for the PATCH HTTP verb?

Something like the following:

@PATCH
public Response someCode() {
    // Code to handle the request
}

Solution

  • I got answer here.

    One will just have to define a custom Patch annotation, what that means is that you will have to write a PATCH.java file with following code:

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @HttpMethod("PATCH")
    public @interface PATCH {
    }
    

    Import the package containing PATCH.java and then you can use it like other HTTP method annotations:

    @PATCH
    @Path("/data/{keyspace}")
    @Produces({ "application/json" })
    public void patchRow(@PathParam("keyspace") String keyspace, String body) 
    throws Exception
    

    I used this @PATCH to send some JSON to my REST service.