2017-01-31 1 views
1

스프링 웹 사이트 https://spring.io/guides/gs/consuming-rest/의 시작 안내서를 따라했습니다.POJO에 직렬화 될 때 RestTemplate이 null을 반환합니다.

다른 엔드 포인트를 사용한다는 점에서 정확한 지침서를 따르지 않습니다. http://www.omdbapi.com?s=rush.

JSON에서 POJO로 변환하는 데 문제가 있습니다. 오류나 예외가 발생하지 않습니다. 누군가 내가 잘못 가고있는 것을 지적 할 수 있습니까? 여기에 여기에 드라이버 클래스 인 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 + "]"; 
    } 
} 

입니다

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

내 POJO에있는 전체 코드를 찾을 수 있습니다.

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()); 
    } 
} 

콘솔 출력은 다음과 같습니다

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

null을 정확히주는 것은 무엇입니까? List 이 null이라는 것을 의미하는 것으로 판단됩니다. 이 경우 가능한 http://stackoverflow.com/questions/19580856/jackson-list-serialization-nested-lists의 복제물 – Redlab

+0

@Redlab 방금 내가 얻는 콘솔 출력을 지정하여 편집했습니다. 나는 RestTemplate이 소비 된 API를 처리하고 POJO로 변환하는 방법을 이해할 수 없기 때문에 중복 된 것이라고 말할 수 없다. – Amriteya

+0

당신은 그것이 중복이 아니라고 맞습니다. 내 대답보기 – Redlab

답변

3

당신은 JSON의 특성에 대해 올바른 식별자 누락, 응답의 차이 수업 자본에와 소문자가있다. 클래스에서 @JsonProperty를 사용하십시오.

@JsonProperty("Search") 
private List<Search> search = new ArrayList<Search>(); 

private String totalResults; 
@JsonProperty("Response") 
private String response; 

검색 클래스에 @JsonProperty 주석을 추가해야합니다.

+0

질문이 있습니다. 왜'totalResults' 필드에 대한 데이터를 얻었습니까? – Amriteya

+2

totalResults case는 json 응답 – mhshimul

+0

과 동일합니다. 왜 지침에 @ annotype @JsonProperty가 사용되지 않고 여전히 작동하고 있습니까? – Amriteya

관련 문제