REST service returns invalid content type and unmarshall

I use RESTEasy and, more specifically, the client side of their framework.

I call the web service of the third part, which returns me the JSON code.

But, for some reason, the content type in their answer is "text / javascript".

How can I tell RESTEasy that it should use a JSON (unmarshalling target) provider for the "text / javascript" content type?

Is it possible?

My code is:

public interface XClient { @GET @Produces("application/json") @Path("/api/x.json") public Movie getMovieInformation( @QueryParam("q") String title); } 

What could be the solution:

 public interface XClient { @GET @Produces("text/javascript") // Tell somehow to use json provider despite the produces annotation @Path("/api/x.json") public Movie getMovieInformation( @QueryParam("q") String title); } 
+2
source share
2 answers

I'm running out of time, so this did the trick for me. I marked the response from the server as String, and I manually process unmarshall with Jackson:

 public interface XClient { @GET @Path("/api/x.json") @Produces(MediaType.APPLICATION_JSON) public String getMovieInformation( @QueryParam("q") String title, } 

and in my REST call:

 MovieRESTAPIClient client = ProxyFactory.create(XClient.class,"http://api.xxx.com"); String json_string = client.getMovieInformation("taken"); ObjectMapper om = new ObjectMapper(); Movie movie = null; try { movie = om.readValue(json_string, Movie.class); } catch (JsonParseException e) { myLogger.severe(e.toString()); e.printStackTrace(); } catch (JsonMappingException e) { myLogger.severe(e.toString()); e.printStackTrace(); } catch (IOException e) { myLogger.severe(e.toString()); e.printStackTrace(); } 

Please advise if this is not the best solution. But it does work.

0
source

I decided to use an interceptor that replaces the type of incoming content, for example:

 this.requestFactory.getSuffixInterceptors().registerInterceptor( new MediaTypeInterceptor()); static class MediaTypeInterceptor implements ClientExecutionInterceptor { @Override public ClientResponse execute(ClientExecutionContext ctx) throws Exception { ClientResponse response = ctx.proceed(); String contentType = (String) response.getHeaders().getFirst("Content-Type"); if (contentType.startsWith("text/javascript")) { response.getHeaders().putSingle("Content-Type", "application/json"); } return response; } } 
+1
source

All Articles