2021年3月13日星期六

Cannot deserialize instance of out of START_OBJECT token - Are my classes the issue or my app file?

Really new to Java/Spring here. I keep receiving an error of "Cannot deserialize". The full error description:

nested exception is   org.springframework.http.converter.HttpMessageNotReadableException:   JSON parse error: Cannot deserialize instance of   `java.util.ArrayList<com.example.consumingrest.Collection>`   out of START_OBJECT token  

This is the data I supposedly need to serialize:

{      "collection": [          {          "id": "1",          "students": [              "Tom",              "Bianka",              "Edwin"            ]          },          {          "id": "2",          "students": [              "Georgiana",              "Dolly",              "Melody",              "Alivia"            ]          }      ]  }  

Collection class looks like this:

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)  public class Collection {        private List<Student> student;        public Collection(List<Student> student) {          this.student = student;      }        @Override      public String toString() {          return "Collection{" +                  "student=" + student +                  '}';      }  }  

Student looks like this:

public class Student {        private String id;      private List<String> students;        public Student(String id, List<String> students) {          this.id = id;          this.students = students;      }        @Override      public String toString() {          return "Student{" +                  "id='" + id + '\'' +                  ", students=" + students +                  '}';      }  }  

And my app file looks like this:

@Bean  public CommandLineRunner run(RestTemplate restTemplate) throws Exception {      return args -> {            ResponseEntity<List<Collection>> response = restTemplate.exchange(                  URL,                  HttpMethod.GET,                  null,                  new ParameterizedTypeReference<List<Collection>>(){});          List<Collection> students = response.getBody();          System.out.println(students);      };  }  

What am I doing wrong here? Also, are my classes the issue or my app file? Thanks!

https://stackoverflow.com/questions/66620097/cannot-deserialize-instance-of-out-of-start-object-token-are-my-classes-the-is March 14, 2021 at 09:03AM

没有评论:

发表评论