Spring Boot 2 WebClient responds to convert JSON to HashMap… here is a solution to the problem.
Spring Boot 2 WebClient responds to convert JSON to HashMap
I want to get the response from WebClient and convert it to Map without creating any classes for the response. Is it possible? So I want something like this. The code below is not valid code, it’s just an idea I want.
public Map<String, String> someFunction() {
return webClient.post()
.uri("/some/path")
.retrieve()
.bodyToFlux(HashMap.class)
.block();
Solution
If you are interested in saving LOCs, you may want to check out the core Spring Framework classes: ParameterizedTypeReference<T>
, Discovery here .
<pre class=”lang-java prettyprint-override”>public Map<String, String> someFunction() {
return webClient.post()
.uri("/some/path")
.retrieve()
.bodyToFlux(new ParameterizedTypeReference<Map<String,String>>(){})
.block();
}
Cheers.