Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement patch requests in RESTEasy?

I would like to implement multiple operations in one Patch request (json format). RESTEasy doesn't support Patch requests out-of-box. How to provide custom implementation?

like image 555
user2932387 Avatar asked Aug 11 '14 23:08

user2932387


1 Answers

To enable PATCH you need to define a annotation annotated with @HttpMethod:

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

A full example is described in Bill Burke's book "RESTful Java with JAX-RS 2.0". The sourcecode can be found in the resteasy repository.

Maybe JAX-RS 2.1. will support PATCH out of the box.

Update: If you want to patch multiple resources in one request you need to identify them first. For instance if you want to give all customers with a certain turnover the VIP status you could have a resource-method like this:

@PATCH
@Path("/customers")
public Response patchCustomers(@QueryParam("minTurnover") Double minTurnover, InputStream is) {
   // find and update customers
}

Which information is passed in the entity body is up to you. The RFC demands "a set of changes" which should be applied to the resource. This could be simple text/plain like update: vip=true. A standard-format for such updates is json-patch:

PATCH /customers?minTurnover=1000 HTTP/1.1
Content-Type: application/json-patch

[
  { 
    "op" : "replace", 
    "path" : "/vip", 
    "value" : "true" 
  },
  {
    ... more operations ...
  }
]

Note that the same set of operations should be applied to all identified resources.

like image 83
lefloh Avatar answered Oct 17 '22 09:10

lefloh