How to use Spring WebClient without Spring Boot

Viewed 3679

I have a very limited need to be able to make HTTP request. I see WebClient is the new replacement for RestTemplate. But it seems it is impossible to use WebClient, without dragging in the whole of spring boot; which is not what I want to do. Any way to use WebClient without Spring boot?

2 Answers

You can make asynchronous HTTP request using Reactor Netty HttpClient (docs). Spring WebClient uses it under the hood. Just add dependency

<dependency>
    <groupId>io.projectreactor.netty</groupId>
    <artifactId>reactor-netty</artifactId>
    <version>0.9.11.RELEASE</version>
</dependency>

and make request

HttpClient.create()
            .request(HttpMethod.GET)
            .uri("http://example.com/")
            .responseContent()
            .asString()
            .subscribe(System.out::println);

I had the same problem and I solved it doing this.

You need to create a logback.xml file in the src / main / resources folder and copy this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <statusListener class="ch.qos.logback.core.status.NopStatusListener" />
</configuration>

If you already have this file just add this statusLIstener inside your configuration.

More info: http://logback.qos.ch/manual/configuration.html

Related