Search code examples
javaspring-bootrestresttemplate

How do I get a JSON object with Spring RestTemplate?


I'm using swapi.dev API to get the data to my application in Spring Boot. I need to get information on a planet using its name. Therefore, I use the next url: https://swapi.dev/api/planets/?search=Tatooine. The JSON result is writing below:

{
    "count": 1, 
    "next": null, 
    "previous": null, 
    "results": [
        {
            "name": "Tatooine", 
            "rotation_period": "23", 
            "orbital_period": "304", 
            "diameter": "10465", 
            "climate": "arid", 
            "gravity": "1 standard", 
            "terrain": "desert", 
            "surface_water": "1", 
            "population": "200000", 
            "residents": [
                "http://swapi.dev/api/people/1/", 
                "http://swapi.dev/api/people/2/", 
                "http://swapi.dev/api/people/4/", 
                "http://swapi.dev/api/people/6/", 
                "http://swapi.dev/api/people/7/", 
                "http://swapi.dev/api/people/8/", 
                "http://swapi.dev/api/people/9/", 
                "http://swapi.dev/api/people/11/", 
                "http://swapi.dev/api/people/43/", 
                "http://swapi.dev/api/people/62/"
            ], 
            "films": [
                "http://swapi.dev/api/films/1/", 
                "http://swapi.dev/api/films/3/", 
                "http://swapi.dev/api/films/4/", 
                "http://swapi.dev/api/films/5/", 
                "http://swapi.dev/api/films/6/"
            ], 
            "created": "2014-12-09T13:50:49.641000Z", 
            "edited": "2014-12-20T20:58:18.411000Z", 
            "url": "http://swapi.dev/api/planets/1/"
        }
    ]
}

Now, in Java, I use the next code in the service:

public PlanetDTO getPlanetByName(String name){
   String url = "https://swapi.dev/api/planets/?search=Tatooine";
   RestTemplate restTemplate = new RestTemplate();
   Object object = restTemplate.getForObject(url, Object.class);
   // I don't know how to get the array of results
}

I only need to get the array of results, but, how do I get the array of results from a Object?


Solution

  • Since you are using Spring Boot, it usually comes bundled with handy tools for JSON parsing. Spring Boot wires per default jackson into your application.

    The first thing, you'll need is a (reduced) POJO model of the response.

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class ResponsePojo {
       @JsonProperty("<jsonFieldName>") // only required, if fieldName != jsonFieldName
       private List<String> residents; 
    
    /* getter & setter ommitted */
    }
    

    In your calling code, use something like

    ResponsePojo response = restTemplate.getForObject(url, ResponsePojo.class);
    response.getResidents() gives you access to the contents of 'resident' array
    

    What happens behind the scenes ?

    RestTemplate sends your request and tries to parse the response into your ResponsePojo object. Since the pojo is a reduced representation of the response, we provided the annotation @JsonIgnoreProperties(ignoreUnknown = true). This tells the parser, that it should simple ignore any field in the json, which cannot be mapped to your pojo. Since a field is provided, with the exact name like in json, the parser is able to identify and map them accordingly.