Spring WebClient 下载 PDF 出现 HTTP 错误

问题描述

在使用 Spring 的 WebClient 从 REST API 检索 PDF 文件时,出现错误

这是 WebClient 的代码

return WebClient.create().get()
              .uri(builder.build().toUri())
              .accept(MediaType.APPLICATION_PDF)
          .exchange()
          .flatMap(response -> response.bodyToMono(byte[].class))
          .block();

我从提供文件的 REST API 收到此错误

03-02-2021 14:32:01.180 [http-nio-8080-exec-6] DEBUG o.s.w.s.m.m.a.httpentityMethodProcessor.writeWithMessageConverters - Found 'Content-Type:application/pdf' in response
03-02-2021 14:32:01.181 [http-nio-8080-exec-6] DEBUG o.s.w.s.m.m.a.httpentityMethodProcessor.traceDebug - Writing [InputStream resource [resource loaded through InputStream]]
03-02-2021 14:32:01.185 [http-nio-8080-exec-6] DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor.afterCompletion - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
03-02-2021 14:32:01.186 [http-nio-8080-exec-6] DEBUG o.s.web.servlet.dispatcherServlet.logResult - Completed 200 OK
03-02-2021 14:32:01.187 [http-nio-8080-exec-6] DEBUG o.a.t.util.net.socketWrapperBase.log - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@d1d3000:org.apache.tomcat.util.net.NioChannel@4b5f2cea:java.nio.channels.socketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:57797]],Read from buffer: [0]
03-02-2021 14:32:01.187 [http-nio-8080-exec-6] DEBUG o.a.coyote.http11.Http11Processor.log - Error parsing HTTP request header
java.io.EOFException: null
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1230)
    at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1140)
    at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:780)
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:356)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
    at org.apache.tomcat.util.net.socketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)
03-02-2021 14:32:01.188 [http-nio-8080-exec-6] DEBUG o.a.coyote.http11.Http11Processor.log - Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@d1d3000:org.apache.tomcat.util.net.NioChannel@4b5f2cea:java.nio.channels.socketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:57797]],Status in: [OPEN_READ],State out: [CLOSED]
03-02-2021 14:32:01.188 [http-nio-8080-exec-6] DEBUG o.a.coyote.http11.Http11NioProtocol.log - Pushed Processor [org.apache.coyote.http11.Http11Processor@1c06182a]
03-02-2021 14:32:01.188 [http-nio-8080-exec-6] DEBUG o.a.tomcat.util.threads.LimitLatch.log - Counting down[http-nio-8080-exec-6] latch=2
03-02-2021 14:32:01.188 [http-nio-8080-exec-6] DEBUG o.apache.tomcat.util.net.NioEndpoint.log - Calling [org.apache.tomcat.util.net.NioEndpoint@6df434e4].closeSocket([org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@d1d3000:org.apache.tomcat.util.net.NioChannel@4b5f2cea:java.nio.channels.socketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:57797]])
03-02-2021 14:32:08.051 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool.logPoolState - HikariPool-1 - Pool stats (total=10,active=0,idle=10,waiting=0)
03-02-2021 14:32:08.051 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool.fillPool - HikariPool-1 - Fill pool skipped,pool is at sufficient level.
03-02-2021 14:32:31.180 [Catalina-utility-2] DEBUG o.a.catalina.session.ManagerBase.log - Start expire sessions StandardManager at 1612359151180 sessioncount 0
03-02-2021 14:32:31.180 [Catalina-utility-2] DEBUG o.a.catalina.session.ManagerBase.log - End expire sessions StandardManager processingTime 0 expired sessions: 0
03-02-2021 14:32:34.196 [http-nio-8080-exec-1] DEBUG o.a.coyote.http11.Http11NioProtocol.log - Processing socket [org.apache.tomcat.util.net.NioChannel@6a7679d5:java.nio.channels.socketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:57771]] with status [ERROR]
03-02-2021 14:32:34.196 [http-nio-8080-exec-1] DEBUG o.a.coyote.http11.Http11NioProtocol.log - Found processor [null] for socket [org.apache.tomcat.util.net.NioChannel@6a7679d5:java.nio.channels.socketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:57771]]
03-02-2021 14:32:34.196 [http-nio-8080-exec-1] DEBUG o.a.tomcat.util.threads.LimitLatch.log - Counting down[http-nio-8080-exec-1] latch=1
03-02-2021 14:32:34.196 [http-nio-8080-exec-1] DEBUG o.apache.tomcat.util.net.NioEndpoint.log - Calling [org.apache.tomcat.util.net.NioEndpoint@6df434e4].closeSocket([org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@1bf3167e:org.apache.tomcat.util.net.NioChannel@6a7679d5:java.nio.channels.socketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:57771]])

知道问题出在哪里吗? 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)