This sample demonstrates a workaround for MessageBodyProviderNotFoundException when handling Content-Type: text/plain responses containing JSON objects using Jersey JAX-RS Client.
When using Jersey JAX-RS library to build a REST client, you want to make sure your RESTful service returns correct Content-Type. However, some web services respond with Content-Type: text/plain, even when returned content is a valid JSON format.
When Jersey JAX-RS attempts to parse such a malformed response, it will throw an error:
javax.ws.rs.client.ResponseProcessingException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=class MyCustomClass, genericType=class MyCustomClass
Let's first look at how Jersey JAX-RS will parse a JSON response with a correct Content-Type: application/json. When it gets such a response, it looks for any available implementation of the javax.ws.rs.ext.MessageBodyReader interface which is annotated as:
@Consumes(MediaType.APPLICATION_JSON)
This can be any implementation, but for our example we added MOXyJsonProvider as a dependency to the project.
Next, we want to make it also handle Content-Type: text/plain responses. For this, we inherit our custom response reader from MOXyJsonProvider at the same time annotating it with MediaType.TEXT_PLAIN:
@Provider
@Consumes(MediaType.TEXT_PLAIN)
public class MyCustomResponseReader extends MOXyJsonProvider {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return genericType.equals(MyCustomClass.class);
}
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
}
}
Note that in the overridden readFrom() method all we do is just call super.readFrom() of the parent class MOXyJsonProvider.
Finally, we need to register our custom reader in the instance of javax.ws.rs.client.Client that will be querying our web service:
Client client = ClientBuilder.newBuilder().build().register(MyCustomResponseReader.class);
Now a text/plain response will be parsed like any application/json one.
I based this solution on the information found in the following resources:
Stack Overflow
Other