无法将JSON响应绑定到GSON注释的POJO

时间:2017-01-04 02:34:34

标签: java json spring gson resttemplate

我打算将JSON字符串绑定到使用GSON注释的POJO,JSON响应来自ReSTFUL服务以列出所有国家/地区:http://services.groupkt.com/country/get/all

响应很好,看起来像

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

1 个答案:

答案 0 :(得分: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);