MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=UTF-8

ashishjmeshram picture ashishjmeshram · Aug 4, 2017 · Viewed 23.5k times · Source

I am trying to use openfire REST-API-Client. I am calling method to add user to the openfire using following code.

AuthenticationToken authenticationToken = new AuthenticationToken("username","password");

RestApiClient restApiClient = new RestApiClient("url",port, authenticationToken);

UserEntity openFireUser = restApiClient.getUser(user.getUsername());

When I call the api I get following exception.

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html;charset=UTF-8, type=class org.igniterealtime.restclient.entity.UserEntity, genericType=class org.igniterealtime.restclient.entity.UserEntity.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
    at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)

I googled and it looks there is some problem with the dependencies. But nothing worked.

Below is my build.gradle

compile(group: 'org.igniterealtime', name :'rest-api-client', version: igniterealtime_rest_api_version){
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }

I also, tried adding following dependencies to my build.gradle, but it did not work.

compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.23'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.2'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.24'
compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '2.4.1'

Answer

Anshul Sharma picture Anshul Sharma · Aug 4, 2017

issue is occurring because mismatch of media type text/html to entity. you can using restful service with media type text/html media type but you need to use media type Application/json. and also check the requested url and media type of request and response.

also use

@Produces("application/json")
@Consumes(MediaType.APPLICATION_JSON)

and make response with proper status code and Mediatype