users@jersey.java.net

[Jersey] Re: JAX-RS/Jersey response 406 with no entity but RFC 2616 recommends to have one

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 14 Dec 2012 01:30:11 +0100

Hello Rodrigo,
if you want to customize the JAX-RS behavior, you can instruct your methods to accept all media types (via @Consumes("*/*")) and do the variant selection by yourself including throwing a custom WAE exception with your custom response message.

You may also file an enhancement request for Jersey to support a better way of the entity generation. We may also consider changing the JAX-RS spec, because with current wording, we simply MUST NOT generate an exception with a non-empty entity. The assertion in JAX-RS specification is unfortunately stronger than the SHOULD assertion in HTTP spec.

Marek

On Dec 13, 2012, at 1:09 PM, Rodrigo Lopes <rdllopes_at_gmail.com> wrote:

> I am working in Java Web Service with Jersey. I'd like to understand how could handle the following problem:
>
> Apparently, JAX-RS (JSR 311) for every situation with 406 Status, say that response will have no entity. For example, in section 3.7.2 Request Matching, Identify the method that will handle the request:
>
> At least one of the acceptable response entity body media types is a supported output data format (see section 3.5). If no methods support one of the acceptable response entity body media types an implementation MUST generate a WebApplicationException with a not acceptable response (HTTP 406 status) and no entity. The exception MUST be processed as described in section 3.3.4.
>
> However, RFC 2616 recommends another thing:
>
> 10.4.7 406 Not Acceptable
>
> The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
>
> Unless it was a HEAD request, the response SHOULD include an entity containing a list of available entity characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. Depending upon the format and the capabilities of the user agent, selection of the most appropriate choice MAY be performed automatically. However, this specification does not define any standard for such automatic selection.
>
> Note: HTTP/1.1 servers are allowed to return responses which are
> not acceptable according to the accept headers sent in the
> request. In some cases, this may even be preferable to sending a
> 406 response. User agents are encouraged to inspect the headers of
> an incoming response to determine if it is acceptable.
> So, what should I do for Jersey include an entity containing a list of available entity characteristics in 406 scenario.
>
> Thanks in advance.
>
> Rodrigo di Lorenzo Lopes