Jersey Client: What if the server side doesn’t return JSON ContentType when it is supposed to?

Your server says it will return JSON string as response, but the ContentType header it sends is not "application/json" but "text/html". What will happen?  

Your client will see an exception saying "A message body reader for … was not found" . 

What you need to do is to have your own "provider" and let it take whatever content type. 

public class MyJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {

	/**
	 * always return true.  
	 */
	@Override
	protected boolean hasMatchingMediaType(MediaType mediaType) {
		return true;
	}
}
//register it with your Jersey client
		restClient = ClientBuilder.newBuilder()
				.register(MyJacksonJaxbJsonProvider.class).build();

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.