0

So I'm having a GET request with webflux as follows:

suspend fun getProperties(): Map<String, Map<String, String>> {
        return webClient
            .get()
            .uri("/property/all")
            .retrieve()
            .bodyToMono(object : ParameterizedTypeReference<Map<String, Map<String, String>>>() {})
            .awaitSingle()
    }

the schema to receive looks as follows:

{
  "additionalProp1": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "additionalProp2": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  },
  "additionalProp3": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  }
}

when call is triggered I'm receiving an error:

"JSON encoding error: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')"

How can I write the custom serialization of nested Maps correctly?

user994612
  • 35
  • 5
  • https://stackoverflow.com/questions/56607372/spring-webflux-webclient-response-convert-list-of-string-to-string/56610277 have a look here – Vova Bilyachat Sep 01 '21 at 00:18

1 Answers1

0

Your response does not looks like Map<String, Map<String, String>> , rather it looks like List<Map<String , String>> , you can update the code to

suspend fun getProperties(): Map<String, Map<String, String>> {
        return webClient
            .get()
            .uri("/property/all")
            .retrieve()
            .bodyToMono(object : ParameterizedTypeReference<List<Map<String , String>>>() {})
            .awaitSingle()
    }
Anshul
  • 1,495
  • 9
  • 17
  • nope.. it is actually Map> (I can see the definition in swagger and read only code..) I just can't change the implementation of it – user994612 Sep 02 '21 at 08:54