Jersey客户端API - 在使用服务时获取JavaIOException过早的EOF

时间:2011-05-19 08:55:48

标签: java jersey

我正在使用Jersey来实现我的web服务。我正在创建一个基于Jersey Client API使用这些服务的Java客户端。当我调用一个web服务来重新生成一个String列表,一个映射或一个业务对象时,ALL就可以了,我在客户端获得了预期的响应。但是,当调用返回String或int的webservice时,我得到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);
}

这是错误stackTrace:

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

1 个答案:

答案 0 :(得分:0)

也可能是多次调用response.getEntity],这意味着您将调用封闭流,因为Closeable阅读器将在第一次调用时关闭。所以只需调用response.getEntity,然后使用实体获取资源值。