Discussion:
Charset from Accept header is not used in MessageBodyWriter#writeTo()
Andreas Kohn
2012-01-11 08:55:50 UTC
Permalink
Hi,

I've been working on supporting multiple charsets in my
MessageBodyWriter.

If I understand correctly then this requires:
1. Request Header 'Accept' would contain the charset, for example
'text/plain;charset=UTF-8'
2. The MessageBodyWriter would check that it can indeed produce this
charset in #isWriteable() based on the MediaType instance
3. The MessageBodyWriteer would properly encode the output in
#writeTo(), again based on the MediaType instance

For a request with 'Accept: text/plain;charset=UTF-8' I indeed get the
charset in #isWriteable().
But in #writeTo() the media type is always "text/plain".

I stepped through the code, and it seems that jersey's
MessageBodyFactory#getMessageBodyWriterMediaType() finds that the most
specific media type given the MBW @Produces("text/plain") and the
acceptable media type of "text/plain;charset=UTF-8" is "text/plain", and
gives this to the #writeTo() method.

Is my approach of detecting the charset wrong, or is this a (known?)
bug?

This is with jersey 1.1.1-ea (historic reasons), but it seems the code
in the latest 1.11 is very similar.


Regards,
Andreas

PS: Does jersey merge the information from the Accept-Charset request
header into the acceptable media type?
--
Never attribute to malice that which can be adequately explained by
stupidity. -- Hanlon's Razor
Loading...