2017-01-04 5 views
0

Я намеревался связать строку JSON с Pojo аннотированный с GSON, ответ JSON это из успокоительной службы перечислить все страны: http://services.groupkt.com/country/get/allНе удалось связать ответ JSON с GSON аннотированный POJO

реакция является штраф, который выглядит как

{ 
    "RestResponse": { 
    "messages": [ 
     "More webservices are available at http://www.groupkt.com/post/f2129b88/services.htm", 
     "Total [249] records found." 
    ], 
    "result": [ 
     { 
     "name": "Afghanistan", 
     "alpha2_code": "AF", 
     "alpha3_code": "AFG" 
     }, 
     { 
     "name": "Åland Islands", 
     "alpha2_code": "AX", 
     "alpha3_code": "ALA" 
     }, 
     ... 
    ] 
    } 
} 

POJO Страна и связанные с ним классы были созданы с помощью этого инструмента: http://www.jsonschema2pojo.org/ и они выглядят как:

Country.java

public class Country implements Serializable{ 
    @SerializedName("RestResponse") 
    @Expose 
    private RestResponse restResponse; 

    public RestResponse getRestResponse() { 
     return restResponse; 
    } 

    public void setRestResponse(RestResponse restResponse) { 
     this.restResponse = restResponse; 
    } 
} 

RestResponse.java

public class RestResponse implements Serializable{ 
    @SerializedName("messages") 
    @Expose 
    private List<String> messages = null; 
    @SerializedName("result") 
    @Expose 
    private List<Result> result = null; 

    public List<String> getMessages() { 
     return messages; 
    } 

    public void setMessages(List<String> messages) { 
     this.messages = messages; 
    } 

    public List<Result> getResult() { 
     return result; 
    } 

    public void setResult(List<Result> result) { 
     this.result = result; 
    } 
} 

Result.java

public class Result implements Serializable{ 
    @SerializedName("name") 
    @Expose 
    private String name; 
    @SerializedName("alpha2_code") 
    @Expose 
    private String alpha2Code; 
    @SerializedName("alpha3_code") 
    @Expose 
    private String alpha3Code; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAlpha2Code() { 
     return alpha2Code; 
    } 

    public void setAlpha2Code(String alpha2Code) { 
     this.alpha2Code = alpha2Code; 
    } 

    public String getAlpha3Code() { 
     return alpha3Code; 
    } 

    public void setAlpha3Code(String alpha3Code) { 
     this.alpha3Code = alpha3Code; 
    } 
} 

Код ниже, однако не удалось связать строку JSON к GSON аннотированный POJO, - restResponse является NULL, так это сообщение и результат. Может ли кто-нибудь сказать мне, что пошло не так?

@SpringBootApplication 
public class App implements CommandLineRunner 
{ 
    private static Logger log = LoggerFactory.getLogger(App.class); 
    /* 
    * boiler plate code 
    * */ 
    public static void main(String[] args) 
    { 
     SpringApplication.run(App.class, args); 
    } 


    /* 
    * Configuration section 
    * */ 
    @Bean 
    public RestTemplate newRestTemplate(){ 
     RestTemplate rt = new RestTemplate(); 
     return rt; 
    } 

    /* 
    * public APIs section 
    * */ 
    @Autowired 
    private RestTemplate restTemplate; 
    @Override 
    public void run(String... args) throws Exception { 
     String url = "http://services.groupkt.com/country/get/all"; 
     ResponseEntity<String> res = restTemplate.getForEntity(url, String.class); 
     log.info("{}",res.getBody()); 


     GsonHttpMessageConverter msgConverter = new GsonHttpMessageConverter(); 
     Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
     msgConverter.setGson(gson); 
     restTemplate.getMessageConverters().add(msgConverter); 

     Country country = restTemplate.getForObject(url, Country.class); 
     RestResponse resp = country.getRestResponse(); 
     List<Result> l = resp.getResult(); 
     for(Result r : l){ 
      log.info("country name = {}",r.getName()); 
     } 
    } 
} 

ответ

0

мне удалось обновить код, как показано ниже, и он работает сейчас:

RestTemplate rt = new RestTemplate(); 
    String url = "http://services.groupkt.com/country/get/all"; 
    ResponseEntity<String> resp = rt.getForEntity(url, String.class); 
    assertEquals(resp.getStatusCode(), HttpStatus.OK); 
    Gson gson = new GsonBuilder().create(); 
    Country c = gson.fromJson(resp.getBody(), Country.class); 

до сих пор не знаю, почему код ниже не работает, хотя.

Country country = restTemplate.getForObject(url, Country.class); 

 Смежные вопросы

  • Нет связанных вопросов^_^