I would like to get a response from WebClient and convert it to just a Map without creating any Class for the response. Is it possible? So I would like something like this below. The code below is not a valid code, it is just an idea of what I want.
public Map<String, String> someFunction() {
return webClient.post()
.uri("/some/path")
.retrieve()
.bodyToFlux(HashMap.class)
.block();
I would first try getting the response object into a String and also make sure I am accepting JSON type in return. Once I get the respone into a String, you can try using fasterxml's jackson databind library which can convert a JSON string into Hashmap.
For example
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"abc\", \"age\":25}";
Map<String, Object> map = new HashMap<String, Object>();
// convert JSON string to Map
map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});
System.out.println(map);
Here is the databind library and core library java docs link
I solved it like this:
public Map<String, String> someFunction() {
return webClient.post()
.uri("/some/path")
.retrieve()
.bodyToFlux(TypedMap.class)
.block();
}
private static class TypedMap extends HashMap<String, String>{}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.