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?
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.
-
Thanks for giving pointers. This implementation is talks about if the request contains only one update. But, I am looking for implementation of a single request contains multiple updates, that may related to different Resources. I can implement in way that parse the input stream in a Common resource and make sequential calls to other resource. But its not elegant solution. I am thinking, a filter/interceptor does the parsing and create/spawn multiple requests. All should run parallel. – user2932387 Aug 12 '14 at 14:58
-
I updated the answer. Note that what happens in the resource class is not JAX-RS related. You could do plain sql-updates or trigger a batch-job which does this with high parallelization. This is nothing the client should decide. He just tells in one request which resources should be updated. – lefloh Aug 12 '14 at 18:26
-
I am looking for solution, if multiple operations are with different paths. – user2932387 Aug 13 '14 at 21:47
-
The path identifies the resource(s) and a HTTP request uses only one. `GET /customers/4711` returns customer 4711 and not customer 0815 or product 1313. Same with `PATCH`: If you want to update e.g. /customers and /products your client needs to make two calls. – lefloh Aug 14 '14 at 05:20
-
I am not sure whether it's a good practice, but 'path' attribute accepts nested paths, so you could imagine making a PATCH request on /customers resource and having multiple operations referring to multiple customers. – botchniaque Aug 20 '14 at 13:04
-
Until one can use the JAX-RS 2.1, one can read https://craftsmen.nl/patching-jax-rs-3/ to see how this can be done now (in Java EE 7 in JBoss for example). – gkephorus Jan 09 '18 at 18:53