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 }
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.
In JAX-RS @Consumes annotation is used to specify the MIME media type ( input) that a resource method can consume from the client.
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.
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.
JAX-RS 2.1 added @PATCH
to the list of supported HTTP methods.
When using Swagger to document a REST API, you could use the existing @PATCH
annotation defined in the io.swagger.jaxrs
package.
Dropwizard defines a @PATCH
annotation in the io.dropwizard.jersey
package.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With