代码之家  ›  专栏  ›  技术社区  ›  Erhannis

工作正常后“找不到MessageBodyReader”

  •  0
  • Erhannis  · 技术社区  · 7 年前

    compile 'org.glassfish.jersey.core:jersey-client:2.25'
    compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.8.3'
    

    我们使用这些调用我们的一个服务器上的REST端点。例如:

    enterSpaceTarget.path(Long.toString(skId)).request(MediaType.APPLICATION_JSON).get();
    

    Form form = new Form();
    form = form.param("username", username);
    form = form.param("password", password);
    form = form.param("rememberMe", Boolean.toString(true));
    loginWebTarget.request().post(Entity.form(form), String.class);
    

    long before = System.currentTimeMillis();
    Long server = pingTarget.request(MediaType.APPLICATION_JSON).get(Long.class);
    long after = System.currentTimeMillis();
    

    我将以最后一个为例继续。看,效果很好。。。大约45分钟。在这种时间尺度下运行许多测试是很困难的,但当你第一次启动应用程序时,一切都很好,如果你一直按按钮,就会持续数小时,但在大约45分钟没有进行web客户端调用后,下一个要进行的调用失败。在第一个示例中,它无声地失败,不引发异常,但在第三个示例中,它引发MessageBodyProviderNotFoundException,如下所示:

    javax.ws.rs.client.ResponseProcessingException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html, type=class java.lang.Long, genericType=class java.lang.Long.
        at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:811)
        at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
        at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
        at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
        at com.....RestClient.ping(RestClient.java:126)
        at com.....VPBCommunicator.lambda$new$3$VPBCommunicator(VPBCommunicator.java:196)
        at com.....VPBCommunicator$$Lambda$5.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:762)
    Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html, type=class java.lang.Long, genericType=class java.lang.Long.
        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.JerseyInvocation.translate(JerseyInvocation.java:806)
        ... 13 common frames omitted
    

    我强烈怀疑这只是一个更深层次问题的表现——正如我所说,其他一些调用只是默默地失败了。有人知道为什么会发生这种情况,以及如何修复它吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Paul Samsotha    7 年前

    未找到媒体类型为text/html的MessageBodyReader

    除非文本/html是您请求的类型,否则几乎所有的时间都意味着服务器端存在错误,并且您将在服务器错误页面上看到什么。您应该检查服务器日志以了解问题所在。

    顺便说一句,最好先检查响应的状态,以便您能够正确处理错误,而不是自动尝试反序列化错误响应并最终获得当前获得的异常

    Response response = target().request().get();
    if (isError(response)) {
       handleError(response);
    } else {
       Long result = response.readEntity(Long.class);
    }