Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Spring WebClient with Jetty, instead of Netty?

According to the documentation it is possible to use the Spring Reactive WebClient with a different server as Netty:

WebClient provides a higher level API over HTTP client libraries. By default it uses Reactor Netty but that is pluggable with a different ClientHttpConnector.

However, I was not able to find a way how to do this. If I simply change the dependency from Netty to Jetty like this:

compile('org.springframework.boot:spring-boot-starter-webflux') {
       exclude group: 'org.springframework.boot', module: 'spring-boot-starter-reactor-netty'
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '2.0.0.M5'

my application will fail to start:

2017-10-30 15:40:43.328 ERROR 20298 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

java.lang.NoClassDefFoundError: reactor/ipc/netty/http/client/HttpClient

Obviously I need to do something more. But this github issue gives me the impression that WebClient cannot be used without Netty.

Is it possible to replace the Netty implementation of WebClient?

like image 542
Martin Drozdik Avatar asked Oct 30 '17 15:10

Martin Drozdik


People also ask

How to use jetty instead of Tomcat in Spring Boot application?

Generally you need to exclude the default embedded Tomcat server that comes with Spring Boot framework and use the Jetty starter in the dependency either in maven’s pom.xml file or gradle’s build.gradle file. So you have seen how to use Jetty instead of Tomcat in Spring Boot application. Now you can build and run your application on Jetty server.

How do I use the spring webclient API?

The Spring WebClient API must be used on top of an existing asynchronous HTTP client library. In most cases that will be Reactor Netty, but you can also use Jetty Reactive HttpClient or Apache HttpComponents, or integrate others by building a custom connector. Once these are installed, you can send your first GET request in WebClient:

What is spring jetty tutorial?

last modified July 9, 2020 Spring Jetty tutorial shows how to run Spring web application on Jetty web server. Springis a popular Java application framework for creating enterprise applications. Jetty

Is it possible to use reactor Netty with spring webclient?

Right now, in Spring Framework, WebClient has only one available ClientHttpConnector implementation, which is powered by Reactor Netty. This explains the current situation - using WebClient means you need Reactor Netty as a dependency.


1 Answers

Add dependency:

org.eclipse.jetty:jetty-reactive-httpclient:1.0.3

And then:

HttpClient httpClient = new HttpClient();
ClientHttpConnector connector = new JettyClientHttpConnector(httpClient);

WebClient webClient = WebClient.builder().clientConnector(connector).build();

Source: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-builder-jetty

like image 65
rgrebski Avatar answered Nov 09 '22 20:11

rgrebski