I am trying to use the Jersey client API to use a third-party REST service. I plan to use automatic POJO deserialization to transition from JSON responses to Java objects.
Unfortunately, the third-party service returns responses using the "text/javascript" content type. My Jersey client does not understand that this should be considered as a JSON object and cannot deserialize the object.
I wrote a simple Jersey server application to make sure that by changing the content type from "text/javascript" to "application/json" , that deserialization works,
Armed with this information, I decided to use the Jersey filter client to change the response headers. The code comes from a comment by the author of this question . In fact, the question seems to be exactly the same as mine, but the respondent incorrectly answered the question and shows how to change the request headers (not the response headers). The original author was able to use the answer to create his solution, but it seems that his claimed solution does not work.
Filter Code:
client.addFilter(new ClientFilter() { @Override public ClientResponse handle(ClientRequest cr) throws ClientHandlerException { ClientResponse response = getNext().handle(cr); response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); return response; } });
When executed, however, an UnsupportedOperationException is UnsupportedOperationException :
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.clear(Collections.java:1035) at com.sun.jersey.core.util.StringKeyIgnoreCaseMultivaluedMap.putSingle(StringKeyIgnoreCaseMultivaluedMap.java:78) at com.sun.jersey.core.util.StringKeyIgnoreCaseMultivaluedMap.putSingle(StringKeyIgnoreCaseMultivaluedMap.java:56) at App$1.handle(App.java:49) at com.sun.jersey.api.client.Client.handle(Client.java:648) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507) at App.main(App.java:63)
The returned headers appear to be wrapped in an unmodifiable collection.
Then I tried to copy all the headers into a new collection, but I see no way to set the header map back to the response.
Finally, I thought that maybe I could create a new ClientResponse containing my changed headers. However, the constructor for ClientResponse has this signature:
public ClientResponse(int status, InBoundHeaders headers, InputStream entity, MessageBodyWorkers workers)
It is trivial to copy the status , headers and entity variables from the original. However, I see no way to get a link to the workers field.
How can I use the Jersey filter client to change the response header from "text/javascript" to "application/json" so that my My POJO deserialization works?