gpt4 book ai didi

java - Jersey 客户端 API - 使用服务时获取 Java IOException 过早的 EOF

转载 作者:行者123 更新时间:2023-12-02 08:12:30 25 4
gpt4 key购买 nike

我正在使用 Jersey 来实现我的网络服务。我正在创建一个 java 客户端,它将使用基于 Jersey Client API 的这些服务。当我调用返回字符串列表、 map 或业务对象的 Web 服务时,一切正常,我在客户端得到了预期的响应。但是,当调用返回 String 或 int 的 Web 服务时,我收到 JavaIOException: 过早的 EOF。请问您对这个问题有什么想法吗?

我以这种方式实例化我的客户端:

String serviceUrl = "http://localhost:9998";
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(config);
resource = client.resource(serviceUrl + "/infos");
resource.accept(MediaType.APPLICATION_JSON_TYPE);

我调用的方法应该返回一个字符串:

public String getInfo() {
return resource.get(String.class);
}

这是错误堆栈跟踪:

com.sun.jersey.api.client.ClientHandlerException: java.io.IOException: Premature EOF
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:563)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:674)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
at org.novapost.proto.client.InfoResourceClient.getInfo(InfoResourceClient.java:37)
at org.novapost.proto.client.TestInfoResourceClient.testGETInfo(TestInfoResourceClient.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:75)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:36)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2391)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.Reader.read(Reader.java:123)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553) ... 25 more

最佳答案

也可能您多次调用 response.getEntity],这意味着您将调用 Closed Stream ,因为 Closeable 阅读器将从第一次调用时关闭。因此,只需调用response.getEntity,然后使用实体来获取资源值。

关于java - Jersey 客户端 API - 使用服务时获取 Java IOException 过早的 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6056100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com