Is there a way to disable automatic JSON PATCH application in Wildfly
max_fichtelmann Jan 5, 2020 4:33 PMWhen using HTTP-PATCH
Resources in JAX-RS on Wildfly (tested with 14 and 18), some automagic happens that calls the same resource path using GET
, applies the changes from the JSON-PATCH and calls the actual method with the result.
The actual ContainerRequestFilter is Resteasy/PatchMethodFilter.java at master · resteasy/Resteasy · GitHub
This is pretty cool in general, however, I have some szenarios in which I am more interested in the patch itself (i.e. generating LDAP modify requests). Is there some way to disable the PatchMethodFilter for specific resources or a single deployment/whole app?
I have created a sample resource class demonstrating the issue (see also my question on stackoverflow.com: java - Is there a way to disable automatic JSON PATCH application in Wildfly - Stack Overflow ):
package so;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonPatch;
import javax.json.JsonStructure;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path( "/" )
public class MyPatchResource
{
public static class Data
{
public String name;
public int age;
}
@GET
@Produces( MediaType.APPLICATION_JSON )
@Path( "auto/{name}" )
public Data read( @PathParam( "name" ) String name )
{
Data result = new Data();
result.name = name;
result.age = 42;
return result;
}
@PATCH
@Path( "auto/{name}" )
@Consumes( MediaType.APPLICATION_JSON_PATCH_JSON )
@Produces( MediaType.APPLICATION_JSON )
public Data patchAuto( Data patched )
{
return patched;
}
@PATCH
@Path( "manual/{name}" )
@Consumes( MediaType.APPLICATION_JSON_PATCH_JSON )
@Produces( MediaType.APPLICATION_JSON )
public JsonObject patchManual( @PathParam( "name" ) String name, JsonStructure patch )
{
if ( !(patch instanceof JsonArray) )
{
throw new WebApplicationException( Response.status( Status.BAD_REQUEST )
.type( MediaType.TEXT_PLAIN )
.entity( "not a json patch: " + patch )
.build() );
}
JsonPatch jsonPatch = Json.createPatch( patch.asJsonArray() );
JsonObject original = readManual( name );
return jsonPatch.apply( original );
}
@GET
@Path( "manual/{name}" )
@Produces( MediaType.APPLICATION_JSON )
public JsonObject readManual( @PathParam( "name" ) String name )
{
return Json.createObjectBuilder()
.add( "name", name )
.add( "age", 42 )
.build();
}
}