2011-05-19 40 views
3

我正在使用Jersey來實現我的webservices。我正在創建一個基於Jersey Client API來使用這些服務的Java客戶端。當我調用一個重新激活字符串列表的Web服務時,地圖或業務對象ALL都可以,我在客戶端獲得預期的響應。然而,當調用返回一個String或一個int的webservice時,我得到了JavaIOException:過早的EOF。 你對這個問題有什麼想法嗎?Jersey客戶端API - 在使用服務時獲取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 

回答

0

也可能是你打電話給response.getEntity]多於一次,這意味着你會打電話關閉流,因爲Closeable閱讀器將從第一次通話中關閉。因此,只需撥打response.getEntity,然後使用該實體獲取資源值。

相關問題