java MessageBodyProviderNotFoundException:未找到 Media type=text/html;charset=UTF-8 的 MessageBodyReader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45498887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:42:53  来源:igfitidea点击:

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

javajerseyopenfirerest-client

提问by ashishjmeshram

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

我正在尝试使用 openfire REST-API-Client。我正在调用使用以下代码将用户添加到 openfire 的方法。

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.

当我调用 api 时,出现以下异常。

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.call(InboundJaxrsResponse.java:115)

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

我用谷歌搜索,看起来依赖项存在一些问题。但没有任何效果。

Below is my build.gradle

下面是我的 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.

我也尝试将以下依赖项添加到我的 build.gradle 中,但没有奏效。

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

回答by Anshul Sharma

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

问题的发生是因为media typetext/htmlto不匹配entity。您可以使用media type text/html媒体类型的宁静服务,但您需要使用media type Application/json. 并检查请求和响应的请求 url 和媒体类型。

also use

也用

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

and make response with proper status code and Mediatype

并用正确的状态代码做出响应和 Mediatype

回答by Denis Abakumov

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:

让我们首先看看 Jersey JAX-RS 将如何解析具有正确 Content-Type: application/json 的 JSON 响应。当它得到这样的响应时,它会寻找 javax.ws.rs.ext.MessageBodyReader 接口的任何可用实现,它被注释为:

@Consumes(MediaType.APPLICATION_JSON)

This can be any implementation, but for our example we added MOXyJsonProvider as a dependency to the project:

这可以是任何实现,但在我们的示例中,我们添加了 MOXyJsonProvider 作为项目的依赖项:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>${jersey.version}</version>
</dependency>

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:

接下来,我们想让它也处理 Content-Type: text/plain 响应。为此,我们从 MOXyJsonProvider 继承我们的自定义响应阅读器,同时使用 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.

请注意,在重写的 readFrom() 方法中,我们所做的只是调用父类 MOXyJsonProvider 的 super.readFrom()。

Finally, we need to register our custom reader in the instance of javax.ws.rs.client.Client that will be querying our web service:

最后,我们需要在将查询我们的 Web 服务的 javax.ws.rs.client.Client 实例中注册我们的自定义阅读器:

Client client = ClientBuilder.newBuilder().build().register(MyCustomResponseReader.class);

Now a text/plain response will be parsed like any application/json one.

现在,文本/纯文本响应将像任何应用程序/json 一样被解析。

GitHub

GitHub

Full solution can be found on GitHub: PlainTextResponseReader

完整的解决方案可以在 GitHub 上找到:PlainTextResponseReader

Credits

学分

I based this solution on the information found in the following resources:

我根据以下资源中的信息制定了此解决方案:

Stack Overflow

堆栈溢出

Other

其他