Search code examples
javaspring-bootresttemplate

RestTemplate returning null when serialized to POJO


I was following the start-up guide of at spring website https://spring.io/guides/gs/consuming-rest/.

I am not following the exact tutorial in the sense that I am using another endpoint: http://www.omdbapi.com?s=rush.

I am having an issue with JSON conversion to POJO. I am not getting any error or exceptions. Could someone point out where am I going wrong?

You can find the complete code here

Here are my POJOs:

package com.sample.restapi.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class SearchResponse {

    private List<Search> search;

    private String totalResults;

    private String response;

    public SearchResponse() {

    }

    public List<Search> getSearch() {
        return search;
    }

    public void setSearch(List<Search> search) {
        this.search = search;
    }

    public String getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(String totalResults) {
        this.totalResults = totalResults;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "SearchResponse [search=" + search + ", totalResults=" + totalResults + ", response=" + response + "]";
    }

}

Here is the Search.java

package com.sample.restapi.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class Search {

    private String title;

    private String year;

    private String imdbID;

    private String type;

    private String poster;

    public Search() {

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getImdbID() {
        return imdbID;
    }

    public void setImdbID(String imdbID) {
        this.imdbID = imdbID;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPoster() {
        return poster;
    }

    public void setPoster(String poster) {
        this.poster = poster;
    }

    @Override
    public String toString() {
        return "Search [title=" + title + ", year=" + year + ", imdbID=" + imdbID + ", type=" + type + ", poster="
                + poster + "]";
    }
}

Here is the driver class.

package com.sample.restapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;

import com.sample.restapi.model.SearchResponse;

@SpringBootApplication
public class ConsumerApplication {

    private static final Logger log = LoggerFactory.getLogger(ConsumerApplication.class);

    public static void main(String[] args) {

        RestTemplate restTemplate = new RestTemplate();
        SearchResponse searchResponse = restTemplate.getForObject("http://www.omdbapi.com?s=rush", SearchResponse.class);

        log.info(searchResponse.toString());
    }
}

The console output is :

14:34:12.941 [main] INFO com.sample.restapi.ConsumerApplication - SearchResponse [search=null, totalResults=344, response=null]

Solution

  • You are missing correct identifiers for the properties in the json, there are differences in the response and your classes in the Capital and lower case letters. Use @JsonProperty in your classes.

    @JsonProperty("Search")
    private List<Search> search = new ArrayList<Search>();
    
    private String totalResults;
    @JsonProperty("Response")
    private String response;
    

    you should also add @JsonProperty annotations in the Search class.