Producing WebClient in a Spring Way

Before we start to call the Twitter APIs, we want to create an instance of WebClient in a Spring way. It means we are looking for a way to inject the instance, using the Dependency Injection Pattern.

To achieve this, we can use the @Configuration annotation and create a WebClient instance, using the @Bean annotation to declare the bean for the Spring container. Let's do that:

package springfive.twittergathering.infra.webimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.Configurationimport org.springframework.web.reactive.function.client.WebClient@Configurationopen class WebClientProducer {    @Bean    open fun webClient(): WebClient? {        return WebClient.create() } ...

Get Spring 5.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.