我目前正在尝试构建一个 API,该 API 从 url 提供图像(以字节数组的形式)。这是我的代码:
@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 获取响应?
@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)
.timeout(Duration.ofSeconds(35));
.block();
return new ResponseEntity(result, HttpStatus.OK);
}
public static void main(String[] args) {
SpringApplication.run(ReturnApplication.class, args);
}
添加以下行
.timeout(Duration.ofSeconds(35));
。
可能调用图片API时超过了默认超时时间