JDK 11 HttpClient integration for Spring WebClient
APACHE-2.0 License
The Java HttpClient for Spring Web Client Boot Starter provides a quick and easy way to use Java 11's HttpClient as Spring WebClient's client HTTP connector.
By default, Spring WebClient will try to use Reactor Netty then Jetty Client as it's client HTTP connector. This starter will instead use Java 11's HTTP client.
Benefits of using this starter include:
To use this starter, you need:
Add a dependency on this starter your dependency manager of choice, making sure to use the latest version:
For Gradle:
dependencies {
runtimeOnly 'com.integralblue:java-httpclient-webclient-spring-boot-starter:VERSION'
}
For Maven:
<dependency>
<groupId>com.integralblue</groupId>
<artifactId>java-httpclient-webclient-spring-boot-starter</artifactId>
<version>VERSION</version>
<scope>runtime</scope>
</dependency>
Follow the advice given in the Spring Boot documentation under Calling REST Services with WebClient when using WebClient
by autowiring an instance of WebClient.Builder
and using that to create the WebClient
instance.
For example:
@Service
public class MyService {
private final WebClient webClient;
public MyService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://example.org").build();
}
public Mono<Details> someRestCall(String name) {
return this.webClient.get().uri("/{name}/details", name)
.retrieve().bodyToMono(Details.class);
}
WebClient.create()
and WebClient.builder().build()
will not apply the customizations from this starter meaning WebClient
s created using those approaches will not use Java 11's HttpClient.
Eventually, a future version of Spring will likely include this functionality. Follow the Pull Request for JDK 11 HttpClient integration with WebClient for more information.
This project requires Java 11 (or later).
Import this Gradle project using your IDE of choice.
Or, if you don't want to use an IDE, you can run the project from the command line: ./gradlew build
The test suite will run and a jar will be output at build/libs/.
The Project Lombok Eclipse integration must be setup. See the Eclipse instructions on Project Lombok's site.