How to handle the wrong JSON content type from web-services using the JAX-RS standard client API?

I want to use the Rotten Tomatoes API to search for movies.

I have an equivalent fully working application that uses TMDB rather than Rotten Tomatoes.

I use the standard JAX-RS Client, provided by JBoss RESTEasy with the RESTEasy Jackson2 provider (I can’t post my API key of course):

public MovieSearchResults search(String query) {
    return client
        .target("http://api.rottentomatoes.com/api/public/v1.0/movies.json")
        .queryParam("apikey", API_KEY)
        .queryParam("q", query)
        .request(MediaType.APPLICATION_JSON)
        .get(MovieSearchResults.class);
}

The MovieSearchResults class is simply a JAXB annotated class to bind the JSON.

The immediate problem is that the Rotten Tomatoes API is returning a response with a content-type of “text/javascript” for all of its JSON responses. They’ve shown a reluctance to change their services even though this is clearly the wrong content-type to set when returning JSON, so right now it is what it is.

The exception I get when I invoke the service is:

Exception in thread "main"
javax.ws.rs.client.ResponseProcessingException: 
javax.ws.rs.ProcessingException: 
Unable to find a MessageBodyReader of 
content-type text/javascript;charset=ISO-8859-1 and type class MovieSearchResults

So the question is: is there a simple way to get/configure the standard JAX-RS Client to recognise the returned “text/javascript” content-type as “application/json”?

These questions are similar, but the accepted answers appear to use JBoss-specific API and I’d like to do it only via the JAX-RS Client API.

CXF JAXRS client not reusing TCP connections

I’m using the JAX-RS support in CXF 2.2.5 to invoke REST webservices. I’m creating a single org.apache.cxf.jaxrs.client.WebClient instance for each endpoint I need to communicate with (typically one o

JAX-RS how to handle wrong content-type

I have a REST service with a @POST method which @Consumes(application/xml). However, if I make a POST request from my browser and I don’t add Content-Type: application/xml header to the request, I

How to handle youtube deleted videos using youtube api from json request

I want to get youtube videos from youtube api using json request. Now i get videos from youtube using json. Example the url have some deleted videos (http://www.youtube.com/watch?v=MlOHWLqgcoY&lis

How to avoid rails from crashing on wrong content type

I am using rails 3.2.8 on Ubuntu 12.04 I am building a web service with json only support. Now if someone sends me data that is not json but sets content-type to application/json the rails application

Force WCF Rest client to use Json deserializer regardless of content-type

How to force WCF Rest client to use Json deserializer regardless of content-type? I am invoking a REST based web service through WCF. The service returns JSON body, but has content-type Application/x

Android+JSON parsing using SOAP webservices

I have a question regarding how to use JSON integration in Android. For example I have a login page I want get authenticated using SOAP webservices to navigate it. I want to use JSON integration, how

How do you Handle a TimeoutException when calling WebServices using the SoapClient in Silverlight?

How do you handle a TimeoutException that is generated when calling WebServices using the SoapClient? There doesn’t seem to be any event to be handled in order to catch any Exceptions generated when c

Using Json Parser for parsing xml type webservices

I have come to know that Json parser is fast over DOM & SAX parser. I also get information that Json parsing using for well formated Javascript type document. My question is there . Can we parse x

JBOSS throwing Exception when trying to Restfull Webservices using JAXRS

I have developed a sample RestEasy Hello World Example for my self assessment, but when i try to run this project using JBOSS7.0 i see a printed stack trace of error mentioned below:Please suggest me

What is wrong w/ parse of JSON data using JavaScript from WordPress & JSON-API

I am trying to parse a JSON file that comes from a WordPress site, using the JSON-API module. I am using JavaScript, JQuery and JQuery Mobile to display a list of poets from an online poetry magazine

Answers

The answer is to use a JAX-RS ClientResponseFilter.

The request is changed to register the filter:

public MovieSearchResults search(String query) {
    return client
        .register(JsonContentTypeResponseFilter.class)
        .target("http://api.rottentomatoes.com/api/public/v1.0/movies.json")
        .queryParam("apikey", API_KEY)
        .queryParam("q", query)
        .request(MediaType.APPLICATION_JSON)
        .get(MovieSearchResults.class);
}

The filter itself simply replaces the content-type header:

public class JsonContentTypeResponseFilter implements ClientResponseFilter {

    @Override
    public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
        List<String> contentType = new ArrayList<>(1);
        contentType.add(MediaType.APPLICATION_JSON);
        responseContext.getHeaders().put("Content-Type", contentType);
    }
}