On Aug 14, 2009, at 9:22 PM, DirkM wrote:
>
> I've noticed that an IllegalArgumentException thrown from a resource
> gets
> turned into a WebApplicationException with a 404 HTTP response code.
>
JAX-RS specifies a 404 should be returned:
https://jsr311.dev.java.net/nonav/releases/1.0/spec/
spec3.html#x3-220003.2
A WebApplicationException thrown during construction of field or
property values using 2 or 3 above is processed
directly as described in section 3.3.4.
Other exceptions thrown during construction of field or property
values using 2 or 3 above are treated as client errors:
if the field or property is annotated with @MatrixParam,
@QueryParam or @PathParam then an implementation
MUST generate a WebApplicationException that wraps the thrown
exception with a not found response (404 status)
and no entity; if the field or property is annotated with
@HeaderParam or @CookieParam then an implementation
MUST generate a WebApplicationException that wraps the thrown
exception with a client error response (400 status)
and no entity. The WebApplicationException MUST be then be
processed as described in section 3.3.4.
The EG opted for a 404 when extracting parameters from URI because
strictly speaking if a String cannot be converted to say Integer then
it is a matching error with the URI.
Paul.
The JAX-RS specification states that
> For example, if I have a service call which takes an int as a
> parameter, and
> I call it as:
> http://localhost:8080/myService/path/countryId=notAnInt
> it will respond with a 404 (Not Found).
>
> Shouldn't it respond with a 400 (Client Error)?
>
> The workaround I have at the moment is to create a custom exception
> mapper
> like this:
>
> @Provider
> @Singleton
> public class WebApplicationExceptionMapper implements
> ExceptionMapper<WebApplicationException> {
> @Override
> public Response toResponse(WebApplicationException e) {
> int statusCode = e.getResponse().getStatus();
>
> //
> // IllegalArgumentExceptions are thrown when the client
> provides an
> // argument to the service that is badly formed, for example
> // countryId=sdfsdf. In that case we want to return an
> // HTTP BAD REQUEST 400. (For some reason 404 is returned by
> default)
> //
> if (e.getCause() instanceof IllegalArgumentException) {
> statusCode = Response.Status.BAD_REQUEST.getStatusCode();
> }
>
> Response.ResponseBuilder builder =
> Response.status(httpStatusCode);
> // (This is a custom class I wrote for nicer JSON output)
> ThrowableResponse entity = new ThrowableResponse(t,
> exceptionStatusCode);
> return
> builder.entity(entity).type(MediaType.APPLICATION_JSON).build();
> }
> }
>
>
> Please let me know if there's something I'm missing here.
> Thanks,
> Dirk
> --
> View this message in context: http://n2.nabble.com/Why-do-IllegalArgumentExceptions-cause-a-404-instead-of-a-400--tp3447019p3447019.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>