【发布时间】:2022-01-13 08:11:56
【问题描述】:
我目前正在尝试构建一个从 url 提供图像(以字节数组的形式)的 API。这是我的代码:
@RestController
@SpringBootApplication
public class ReturnApplication {
private static final Logger logger = Logger.getLogger(ReturnApplication.class.getSimpleName());
@Autowired
private final WebClient.Builder webClientBuilder;
public ReturnApplication() {
this.webClientBuilder = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().followRedirect(true).wiretap(true)
));
}
@GetMapping("/quotes")
public ResponseEntity<?> getRandomQuote() {
logger.info("Get Random Quote");
Object result = WebClient.create()
.get()
.uri("https://api.quotable.io/random")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Object.class)
.block();
return new ResponseEntity(result, HttpStatus.OK);
}
public static void main(String[] args) {
SpringApplication.run(ReturnApplication.class, args);
}
}
但是,每次我尝试点击该服务时,都会出现这种错误:
java.net.ConnectException: Connection timed out: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_201]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_201]
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]
我尝试调试网络客户端,但它总是让我超时。我可以在浏览器中顺利打开网址。我的代码实际上有什么问题,我该如何解决?
编辑
我决定再做一个spring boot api项目来测试是不是Webclient的问题,其实不是。之后,我尝试从命令行 ping 那个 API url(我在上面使用的那个),它一直超时。我也不能从 CURL 发出请求。我可以通过 Postman 或网络浏览器顺利完成请求。因此,我应该怎么做才能使 Spring Webclient 能够从该 API 获得响应?
【问题讨论】:
-
这个应用程序部署在哪里,你能从那个位置连接到
api.quotable.io吗? -
不要使用
block(),这违背了使用 Spring WebFlux 的全部目的。 -
@MichaelMcFadyen 我可以通过浏览器和邮递员连接到
api.quotable.io,但我不能从 spring、curl 和 ping 连接。 -
@JoãoDias 你能给我更多的建议吗?我只是不知道如何为该服务的 json 输出变异/添加另一个响应字段...
-
这听起来像是网络问题,而不是 app/code/webflux 问题。我建议关闭这个问题,因为它不再准确。
标签: spring-boot httprequest spring-webflux spring-webclient