Spring Boot WebClient class is a non-blocking, reactive client to perform HTTP requests.
The WebClient class is a core component in Spring Boot applications for making non-blocking and reactive HTTP requests. It’s designed as the successor to the older RestTemplate and offers several advantages:
Reactive approach: WebClient leverages the reactive programming paradigm, making it efficient in handling asynchronous operations and working well with event-driven systems. This can significantly improve the performance of your application, especially under high load.
Non-blocking: It avoids blocking the main thread while waiting for responses, allowing your application to remain responsive and handle other tasks concurrently.
Flexibility: It supports both synchronous and asynchronous operations, catering to various application needs. Builder pattern: It uses a fluent builder pattern for configuring requests, making the code more readable and maintainable.
Dependency
To use WebClient in your Spring Boot application, you need to add the spring-boot-starter-webflux dependency to your pom.xml file:
Use retrieve() method to declare how to extract the response body. retrieve() method returns ResponseSpec which provides methods to extract the response body.
use toEntity() method to get the response body and headers.
You can use filter() method to add filters to the WebClient. For example, you can add logging filter to log the request and response. filter() method takes an ExchangeFilterFunction as an argument.
@Test voidgetPost()throws IOException, InterruptedException { PostmockResponsePost=newPost() .setUserId(1) .setTitle("My first post") .setBody("Mock Post Body");
// stub some responses. mockBackEnd.enqueue(newMockResponse() .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .setBody(newObjectMapper().writeValueAsString(mockResponsePost)));
// Call the method. Postresult= demoService.createPost(newPost().setUserId(1).setTitle("My first post").setBody("Mock Post Body"));
// Verify the result assertEquals(mockResponsePost.getBody(), result.getBody());
// Optional - verify the requests. RecordedRequestrecordedRequest= mockBackEnd.takeRequest(); assertEquals("POST", recordedRequest.getMethod()); assertEquals("/posts", recordedRequest.getPath()); assertEquals("{\"id\":null,\"title\":\"My first post\",\"body\":\"Mock Post Body\",\"userId\":1}", recordedRequest.getBody().readString(Charset.defaultCharset())); } }
Dispatch
By default MockWebServer uses a queue to specify a series of responses. We can also use a Dispatcher (import okhttp3.mockwebserver.Dispatcher) to handle requests using policy.