I am trying to create a Spring WebClient
to call REST API. For that, I wrote the following piece of code.
@Bean
public WebClient defaultWebClient() {
var tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3_000)
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(3))
.addHandlerLast(new WriteTimeoutHandler(3)));
return WebClient.builder()
.baseUrl("https://test.com")
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.defaultHeader(HttpHeaders.ACCEPT, "application/vnd.api+json")
.build();
}
then the actual URI with the following code.
String user = webClient.get()
.uri(URI.create("/api/v1/user/1"))
.header("Authorization", TOKEN)
.header(HttpHeaders.ACCEPT, "application/json")
.retrieve()
.bodyToMono(String.class)
.block();
but when the above code executed then it throws the following exception.
{"timestamp":"1608209276546","level":"ERROR","thread":"http-nio-8080-exec-1","mdc":{"tenantId":null},"logger":"org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]","message":"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: no further information: /127.0.0.1:12012; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:12012] with root cause","context":"default","exception":"java.net.ConnectException: Connection refused: no further information\r\n\tat java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)\r\n\tat
as raw too:
{"timestamp":"1608209276546","level":"ERROR","thread":"http-nio-8080-exec-1","mdc":{"tenantId":null},"logger":"org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]","message":"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: no further information: /127.0.0.1:12012; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:12012] with root cause","context":"default","exception":"java.net.ConnectException: Connection refused: no further information\r\n\tat java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)\r\n\tat
I am not sure where I need to change here so that it starts pointing to base URI instead of localhost (127.0.0.1:12012). Another thing is if I pass the fully qualified name (https://test.com/api/v1/user/1) on API call then it works fine.