Dhana D.
Dhana D.

Reputation: 1720

Spring Webclient always gives error timed out

I am currently trying an experiment to build an API that gives an image (in the form of byte array) from the url. This is my code:

@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);
    }

}

However, everytime I try hitting that service it gives this kind of error:

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]

I tried debugging the webclient, but it always gives me time out. I can open the url in the browser smoothly. What is actually wrong in my code and how can I fix this??


Edit

I decide to make another spring boot api project to test whether it's the Webclient problem, but it actually isn't. After that, I tried to ping that API url (the one I used above) from the command line, it got timed out all the time. I can't do the request from CURL as well. I can just do the request smoothly through Postman or web browser. Therefore, what should I do to make Spring Webclient able to get response from that API?

Upvotes: 0

Views: 4235

Answers (1)

Yaseen
Yaseen

Reputation: 98

@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);
    }

add the following line .timeout(Duration.ofSeconds(35));. Maybe the default time-out exceeds when invoking the image API

Upvotes: 0

Related Questions