При весенней загрузке возникает ошибка при анализе json RestTemplate.getForObject

#java #spring-boot

Вопрос:

Я хочу проанализировать эту строку json, но я получаю ошибку, я пробовал по-другому, но это не работает

Моя ошибка

Вызвано: com.fasterxml.jackson.databind.exc.Несовпадение входных данных Исключение: Не удается десериализовать экземпляр arrest_warrants маркера out of START_ARRAY

 {
  "arrest_warrants": [
    {
      "issuing_country_id": "CO",
      "charge": "dfdfd",
      "charge_translation": null
    }
  ],
  "weight": 0,
  "forename": "MOISES",
  "date_of_birth": "1965/06/02",
  "entity_id": "2021/40054"
}
 

Вызов синтаксического анализа: я получаю ошибку в строке ниже

 Interpoldetails intterdt = restTemplate.getForObject("xxxurl" id, Interpoldetails.class);
 

Мой класс

 @JsonIgnoreProperties(ignoreUnknown = true)
public class Interpoldetails {
    private Integer weight;
    private String date_of_birth;
    private String entity_id;
    private String name;
    private arrest_warrants arrest_warrants;
    private String[] languages_spoken_ids;
    private Integer height;
    private Integer sex_id;
    private String country_of_birth_id;
    private String place_of_birth;
    public Integer getWeight() {
        return weight;
    }
    public void setWeight(Integer weight) {
        this.weight = weight;
    }
    public String getDate_of_birth() {
        return date_of_birth;
    }
    public void setDate_of_birth(String date_of_birth) {
        this.date_of_birth = date_of_birth;
    }
    public String getEntity_id() {
        return entity_id;
    }
    public void setEntity_id(String entity_id) {
        this.entity_id = entity_id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String[] getLanguages_spoken_ids() {
        return languages_spoken_ids;
    }
    public void setLanguages_spoken_ids(String[] languages_spoken_ids) {
        this.languages_spoken_ids = languages_spoken_ids;
    }
    public Integer getHeight() {
        return height;
    }
    public void setHeight(Integer height) {
        this.height = height;
    }
    public Integer getSex_id() {
        return sex_id;
    }
    public void setSex_id(Integer sex_id) {
        this.sex_id = sex_id;
    }
    public String getCountry_of_birth_id() {
        return country_of_birth_id;
    }
    public void setCountry_of_birth_id(String country_of_birth_id) {
        this.country_of_birth_id = country_of_birth_id;
    }
    public String getPlace_of_birth() {
        return place_of_birth;
    }
    public void setPlace_of_birth(String place_of_birth) {
        this.place_of_birth = place_of_birth;
    }
    public arrest_warrants getArrest_warrants() {
        return arrest_warrants;
    }
    public void setArrest_warrants(arrest_warrants arrest_warrants) {
        this.arrest_warrants = arrest_warrants;
    }
    
    
}
 

Мой внутренний класс

 @JsonIgnoreProperties(ignoreUnknown = true)
public class arrest_warrants {
    private String issuing_country_id;
    private String charge;
    public String getIssuing_country_id() {
        return issuing_country_id;
    }
    public void setIssuing_country_id(String issuing_country_id) {
        this.issuing_country_id = issuing_country_id;
    }
    public String getCharge() {
        return charge;
    }
    public void setCharge(String charge) {
        this.charge = charge;
    }
}
 

Комментарии:

1. arrest_warrants-это массив, а не объект

2. @shikida меняет закрытую строку на[] arrest_warrants; но все равно получает ошибку

3. org.springframework.http.конвертер. Исключение HttpMessageNotReadableException: Ошибка синтаксического анализа JSON: Не удается десериализовать экземпляр java.lang.String из маркера START_OBJECT;

4. в ваших классах есть несколько недостающих полей. перепроверьте их

5. @шикида, спасибо, проблема решена по вашему предложению.

Ответ №1:

это должно быть arrest_warrants[] , а не строка[]

Ответ №2:

Поскольку arrest_warrants-это массив объектов, вы можете создать объект arrest_warrant и в классе Interpoldetails использовать private List<arrest_warrant> arrest_warrants