erp
erp

Reputation: 3014

Jersey client with null put method

I am working on a Jersey service client for one of my services and am having trouble determining the best way to pass a null entity through the client's put. On the service side of things this is my endpoint:

  @PUT
  @Path("/rule/disable/key/{key}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response disableRuleByKey(@PathParam("key") String key)
      throws Exception {
    try {
      DAL.getWriter().disableRuleByKey(key);
      return Response.ok().build();
    } catch (BlahException bla) {
      throw de;
  }

Basically all the method does in the backend is flip a toggle for other parts of the application to use. I'm not sure if put is the correct call to use here (but this was written by a teammate). I know it doesn't even have a JSON payload.

Anyways, on the client side I have this generic putItem() code for all of my clients to use via extends:

  public static <T> boolean putItem(Client client, String uri, T item)
      throws InterruptedException,
      ExecutionException {
    Invocation putConfig = client.target(uri).request()
        .buildPut(Entity.entity(item, MediaType.APPLICATION_JSON));
    Future<Response> asyncResponse = putConfig.submit();
    Response response = asyncResponse.get();

    return response.getStatus() == Status.OK.getStatusCode();
  }

This PUTs into the database fine with a JSON payload, but since the method above doesn't specifically have a payload I was wondering what the best course of action would be. Would modifying the Invocation's .buildPut() to have null in it be okay since I am not passing in a payload.

I am open to modifying the endpoint too but this is what I currently have and can't figure out the best way to send this value to the backend. Should I just modify the endpoint to consume a JSON object rather than passing the key as a @PathParam?

Upvotes: 1

Views: 834

Answers (1)

cassiomolin
cassiomolin

Reputation: 130977

When replacing the state of a resource with a PUT request, you should send the new representation in the request payload.

Have a look the the RFC 7231, the current reference for semantics and content in HTTP/1.1:

4.3.4. PUT

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. [...]

Upvotes: 1

Related Questions