"MessageBodyReader not found" 工作正常后
"MessageBodyReader not found" after working fine
因此,我们正在开发 Android 应用程序。在我们的 gradle 进口中有以下:
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 分钟没有进行任何网络客户端调用后,下一次调用失败。在第一个例子中,它默默地失败了,没有抛出异常,但是在第三个例子中,它抛出了一个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.access0(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation.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$VPBCommunicator(VPBCommunicator.java:196)
at com.....VPBCommunicator$$Lambda.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
我强烈怀疑这只是更深层次问题的表现——正如我所说,其他一些调用只是默默地失败了。有谁知道为什么会发生这种情况,以及如何解决它?
MessageBodyReader not found for media type=text/html
除非 text/html 是您请求的类型,几乎所有时候这都意味着服务器端有错误,以及您得到的服务器错误页面。您应该检查服务器日志以查看问题所在。
顺便说一句,首先检查响应的状态以确保您可以正确处理错误总是一个好主意,而不是自动尝试反序列化错误响应并最终得到您当前的异常得到
Response response = target().request().get();
if (isError(response)) {
handleError(response);
} else {
Long result = response.readEntity(Long.class);
}
因此,我们正在开发 Android 应用程序。在我们的 gradle 进口中有以下:
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 分钟没有进行任何网络客户端调用后,下一次调用失败。在第一个例子中,它默默地失败了,没有抛出异常,但是在第三个例子中,它抛出了一个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.access0(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation.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$VPBCommunicator(VPBCommunicator.java:196)
at com.....VPBCommunicator$$Lambda.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
我强烈怀疑这只是更深层次问题的表现——正如我所说,其他一些调用只是默默地失败了。有谁知道为什么会发生这种情况,以及如何解决它?
MessageBodyReader not found for media type=text/html
除非 text/html 是您请求的类型,几乎所有时候这都意味着服务器端有错误,以及您得到的服务器错误页面。您应该检查服务器日志以查看问题所在。
顺便说一句,首先检查响应的状态以确保您可以正确处理错误总是一个好主意,而不是自动尝试反序列化错误响应并最终得到您当前的异常得到
Response response = target().request().get();
if (isError(response)) {
handleError(response);
} else {
Long result = response.readEntity(Long.class);
}