Map Nested JSON Objects to Java Classes with Spring RestTemplate

前端 未结 7 1341
南旧
南旧 2021-02-08 12:17

I know this may be simple. However, I just can\'t get it to work.

So I am trying to use Spring RestTemplate to map my JSON data. I have following JSON response from a re

7条回答
  •  我在风中等你
    2021-02-08 12:52

    I was having a similar issue with RestTemplate not mapping nested JSON objects to my class model also, after much frustration I decided to retrieve my JSON as a String(instead of converting directly to my target object) using RestTemplate and then use the google Gson library to convert my String to my target entity.

    pom.xml

    
            com.google.code.gson
            gson
            2.2.4
        
    

    code to call RestTemplate:

     ResponseEntity responseEntity = restTemplate.getForEntity(Url,String.class);
    Gson gson = new GsonBuilder().create();
    Response reponse = gson.fromJson(responseEntity , Response.class);
    

    Unfortunately I was unable to find out why my nested objects were not mapped using RestTemplate the first place but I hope this workaround helps!

提交回复
热议问题