users@jersey.java.net

Re: [Jersey] java.lang.IllegalArgumentException: Error parsing media type

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 18 Jun 2009 08:34:30 +0200

On Jun 17, 2009, at 7:33 PM, Michael Andrews wrote:

> Hi Paul,
>
> Thanks for your quick reply.
>
>> Hi Michael,
>>
>> The media type "text/plain;javascript" is not a valid media type as
>> defined by the HTTP 1.1 specification [1].
>>
>> Parameters require a value. Note that content negotiation will only
>> take into account the type and subtype of the media type and not any
>> parameters.
>
> I figured as much, but I have little control over enforcing the
> specification since it is an external URI. Basically the service
> returns
> 'application/json' except that it is labeled 'text/
> plain;javascript'. Is
> there a technique in the API to rewrite this header before the Client
> assigns a Provider?
>

Yes, you can utilize a filter to modify the Content-Type from "text/
plain;javascript" to a well formed media type:

   https://jersey.dev.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/client/filter/ClientFilter.html

and you can add an instance of your filter to the Client or
WebResource using the addFilter method.

In your filter you can set the Content-Type by getting the headers.

      class AppClientFilter extends ClientFilter {

          public ClientResponse handle(ClientRequest cr) {
              // Call the next client handler in the filter chain
              ClientResponse resp = getNext().handle(mcr);

              String contentType = resp.getHeaders().getFirst("Content-
Type");
              String newContentType = ...
              resp.getHeaders().putSingle("Content-Type",
newContentType);
              return resp;
          }
      }

Paul.