【问题标题】:Failed to bind JSON response to GSON annotated POJO无法将 JSON 响应绑定到 GSON 注释的 POJO
【发布时间】:2017-01-04 02:34:33
【问题描述】:

我打算将一个 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 Country 及其关联类是使用此工具创建的: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;
    }
}

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

【问题讨论】:

    标签: java json spring gson resttemplate


    【解决方案1】:

    我设法更新了如下代码,它现在可以工作了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多