Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 } 
like image 861
VishalDevgire Avatar asked Jul 27 '13 11:07

VishalDevgire


People also ask

What is the use of @produce annotation of JAX-RS?

The @Produces AnnotationIf applied at the method level, the annotation overrides any @Produces annotations applied at the class level. If no methods in a resource are able to produce the MIME type in a client request, the JAX-RS runtime sends back an HTTP “406 Not Acceptable” error.

What is the use of @consumes annotation of JAX-RS?

In JAX-RS @Consumes annotation is used to specify the MIME media type ( input) that a resource method can consume from the client.

What is the use of at the rate delete annotation of JAX-RS?

The @DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.


2 Answers

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.

like image 88
VishalDevgire Avatar answered Sep 28 '22 09:09

VishalDevgire


Using JAX-RS 2.1?

JAX-RS 2.1 added @PATCH to the list of supported HTTP methods.

Using Swagger?

When using Swagger to document a REST API, you could use the existing @PATCH annotation defined in the io.swagger.jaxrs package.

Using Jersey and Dropwizard?

Dropwizard defines a @PATCH annotation in the io.dropwizard.jersey package.

Write your own

If the above mentioned approaches don't work for you, you can write your own @PATCH annotation:

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

The @HttpMethod annotation is used to associate the name of a HTTP method with an annotation, creating a what the JAX-RS specification calls resource method designator.

Your own @PATCH annotation should work fine in Swagger.

like image 27
cassiomolin Avatar answered Sep 28 '22 11:09

cassiomolin