无法将json响应绑定到gson注解的pojo

r1zhe5dt  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(356)

我打算将一个json字符串绑定到用gson注解的pojo,json响应来自restful服务以列出所有国家:http://services.groupkt.com/country/get/all
React很好,看起来

{
  "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 country及其相关类是使用tool:http://www.jsonschema2pojo.org/ 它们看起来像:
国家.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;
    }
}

ResResponse.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;
    }
}

结果.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-response为空,消息和结果也是空的。谁能告诉我哪里出了问题吗?

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

vfhzx4xs1#

我成功地更新了下面的代码,现在可以工作了:

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

相关问题