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?
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.
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