Почему клиент Джерси выдает ошибку чтения сущности из входного потока - PullRequest
0 голосов
/ 08 апреля 2020

Я недавно создал приложение с использованием jersey / jax-rs, выступающего в роли клиента. Иногда приложение работает хорошо. Но большую часть времени я получаю эту ошибку при выполнении readEntity (String.class), даже несмотря на то, что статус ответа возвращает код «200».

17:38:37.608 [main] DEBUG Read URL: https://example.com/abc&page=3&page_size=10000
17:39:21.278 [main] DEBUG Status code: 200
17:41:42.837 [main] DEBUG Disconnecting from database: :memory:
17:41:42.839 [main] DEBUG class com.spacey.processor.impl.AProcessor Finished in 338 seconds
17:41:42.840 [main] ERROR Error! javax.ws.rs.ProcessingException: Error reading entity from input stream.
java.lang.RuntimeException: javax.ws.rs.ProcessingException: Error reading entity from input stream.
        at com.spacey.processor.impl.AProcessor.process(AProcessor.java:141)
        at app.Executor.run(Executor.java:52)
        at app.Main.run(Main.java:33)
        at app.Main.main(Main.java:67)
Caused by: javax.ws.rs.ProcessingException: Error reading entity from input stream.
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:867)
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785)
        at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96)
        at org.glassfish.jersey.client.ScopedJaxrsResponse.access$001(ScopedJaxrsResponse.java:56)
        at org.glassfish.jersey.client.ScopedJaxrsResponse$1.call(ScopedJaxrsResponse.java:77)
        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:397)
        at org.glassfish.jersey.client.ScopedJaxrsResponse.readEntity(ScopedJaxrsResponse.java:74)
        at com.spacey.impl.RestInvoker.readUrl(RestInvoker.java:89)
        at com.spacey.impl.RestInvoker.read(RestInvoker.java:62)
        at com.spacey.impl.RestInvoker.read(RestInvoker.java:71)
        at com.spacey.impl.AProcessor.readObjects(AProcessor.java:157)
        at com.spacey.impl.AProcessor.process(AProcessor.java:130)
        ... 3 more
Caused by: javax.net.ssl.SSLException: SSL peer shut down incorrectly
        at sun.security.ssl.InputRecord.readV3Record(Unknown Source)
        at sun.security.ssl.InputRecord.read(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
        at sun.security.ssl.AppInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        at sun.net.www.http.ChunkedInputStream.fastRead(Unknown Source)
        at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
        at java.io.FilterInputStream.read(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
        at org.glassfish.jersey.client.HttpUrlConnector$1.read(HttpUrlConnector.java:157)
        at org.glassfish.jersey.message.internal.EntityInputStream.read(EntityInputStream.java:101)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.Reader.read(Unknown Source)
        at org.glassfish.jersey.message.internal.ReaderWriter.readFromAsString(ReaderWriter.java:172)
        at org.glassfish.jersey.message.internal.ReaderWriter.readFromAsString(ReaderWriter.java:158)
        at org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:115)
        at org.glassfish.jersey.message.internal.StringMessageProvider.readFrom(StringMessageProvider.java:77)
        at org.glassfish.jersey.message.internal.StringMessageProvider.readFrom(StringMessageProvider.java:59)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:239)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:211)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:139)
        at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1109)
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
        ... 17 more
17:41:43.061 [main] INFO  Done in 339202ms

Вот как выглядит мой код:

private List<JsonNode> readUrl(){
  javax.ws.rs.client.Client client = ClientBuilder.newClient();
  String restUrl = restUrl;
  javax.ws.rs.client.WebTarget target = client.target(restUrl);
  javax.ws.rs.core.Response response = target.request(MediaType.APPLICATION_JSON).accept(acceptType).header(HttpHeaders.AUTHORIZATION, "xyz-abc").get();
  final int statusCode = response.getStatus();
  LOGGER.debug("Status Code: " + statusCode);
  String responseStr = response.readEntity(String.class); // exception gets thrown here
}

Я не понимаю, в чем здесь ошибка. И вот как выглядит мой пом. xml:

         <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <java.version>1.8</java.version>
           <jersey.version>2.3.1</jersey.version>
           <jackson.version>2.9.3</jackson.version>
           <junit.version>4.12</junit.version>
        </properties>
        <!-- Jersey Client -->

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- Jackson -->

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
...