是否可以在Wildfly中禁用自动JSON PATCH应用程序

问题描述 投票:1回答:1

在Wildfly上的JAX-RS中使用HTTP-PATCH资源时(经过14和18的测试),会发生一些自相矛盾的事情,即使用GET调用相同的资源路径,应用JSON-PATCH中的更改并使用结果。

$ curl localhost:8080/so/auto/Bob -H 'Content-Type: application/json-patch+json' -X PATCH -d '[{"op":"replace","path":"/age","value":24}]' -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PATCH /so/auto/Bob HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json-patch+json
> Content-Length: 43
>
* upload completely sent off: 43 out of 43 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Transfer-Encoding: chunked
< Content-Type: application/json
< Date: Tue, 26 Nov 2019 12:45:51 GMT
<
* Connection #0 to host localhost left intact
{"age":24,"name":"Bob"}

是否有一种方法可以禁用此行为(使#patchManual正常工作?

$ curl localhost:8080/so/manual/Bob -H 'Content-Type: application/json-patch+json' -X PATCH -d '[{"op":"replace","path":"/age","value":24}]' -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PATCH /so/manual/Bob HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json-patch+json
> Content-Length: 43
>
* upload completely sent off: 43 out of 43 bytes
< HTTP/1.1 400 Bad Request
< Connection: keep-alive
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 41
< Date: Tue, 26 Nov 2019 12:42:58 GMT
<
* Connection #0 to host localhost left intact
not a json patch: {"name":"Bob","age":24}

重现该问题的代码:

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();
    }
}

java jax-rs wildfly json-patch
1个回答
0
投票

[命中readManual()端点时,您正在此行JsonObject original = readManual( name );呼叫patchManual()

© www.soinside.com 2019 - 2024. All rights reserved.