I have the following JSON Response:
{
"Count": 1,
"Products": [
{
"ProductID": 3423
},
{
"ProductID": 4321
}
]
}
I want to be able to return a List of "Product" from the Products array using WebClient without having to create a separate Dto class with the field 'ArrayList products'
I used something like this
webClient.get()
.uri(uriBuilder -> uriBuilder
.path(URI_PRODUCTS)
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlux(Product.class)
.collectList();
It retrieves a List with one Product in it but all the values null. I am able to get it to work with a DTO response such as
...retrieve().bodyToMono(ProductResponse.class).block();
Where the ProductResponse has the List of Products in it. But I am trying to avoid having to create the extra class. Is there a way to pull the field similar to using jsonPath (similar to WebTestClient)?
You would just use bodyToMono which would return a Mono object. WebClient webClient = WebClient. create(); String responseJson = webClient. get() .
Logging Request and Response with Body HTTP clients have features to log the bodies of requests and responses. Thus, to achieve the goal, we are going to use a log-enabled HTTP client with our WebClient. We can do this by manually setting WebClient. Builder#clientConnector – let's see with Jetty and Netty HTTP clients.
RestTemplate uses Java Servlet API and is therefore synchronous and blocking. Conversely, WebClient is asynchronous and will not block the executing thread while waiting for the response to come back. The notification will be produced only when the response is ready. RestTemplate will still be used.
after retrieve()
you can always .map
your result to corresponding type. With the help of JsonNode
path()
instance method you can do it similar to WebTestClient
jsonPath()
webClient.get()
.uri(uriBuilder -> uriBuilder
.path(URI_PRODUCTS)
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(JsonNode.class)
.map(s-> s.path("Products"))
.map(s->{
try {
return mapper.readValue(s.traverse(), new TypeReference<List<Product>>() {} );
} catch (IOException e) {
e.printStackTrace();
return new ArrayList<Product>();
}
})
.block();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With