Hi,

I have a resource that takes a POST request with JSON payload. Let's say it
takes an *int[]*, and for our purposes simply returns it. There are (at
least) two ways to do this:

(1) Accept an *int[]*, letting Jackson auto-deserialize it. For echoing,
return the deserialized array.

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public int[] postObject(final int[] userData) {
    return userData;
  }


(2) This version accepts a *byte[]*, and then internally deserializes it to
the *int[]* object to be returned.

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public int[] postObject(final byte[] userData) {
    ObjectMapper mapper = new ObjectMapper();
    try {
      return mapper.readValue(userData, int[].class);
    } catch (Exception e) {
      throw new WebApplicationException(Status.BAD_REQUEST);
    }
  }


Suppose that the request is not deserializable to an *int[]*. E.g.,

[1, 2, 'a']

or

[1, 2,

?

In method 1, the deserialization error will result in a *null* argument
passed to the *postObject* function and the user will get a *204 NO CONTENT*
 response.

In method 2, the deserialization error will result in a catchable
exception, and the client will now get a *400 BAD REQUEST* response.

Is there a way to make method 1 result in a 400 error? Is there a handler
function for those types exceptions? It is quite bulky to have to wrap
every single API call like method 2, but the 204 response is just wrong.

Thanks,
Dan

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3054619

Reply via email to