Search code examples
javajsonpostmanrestful-architecture

Error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Entities.Student out of START_ARRAY token


I have a 'JudoClass' object that contains an arrayList of 'Student' objects. When I try to create a student, I get the above error.

Post method:

  @POST
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
 @Path("/createStudent")
 public Response createAccount(Student aStudent) {
  students.put(aStudent.getId(),  aStudent);
  allStudents.add(aStudent);
  System.out.print("user created with id: " + aStudent.getId());
  return Response.ok(students, MediaType.APPLICATION_JSON).build();   
 }

students is a hash map of all the students. (allStudents is an arrayList, i'm testing with both)

Json in postman:

 [
{
    "id": 3,
    "username": "Mark",
    "password": "password"
}
]

I also get this error when I try to create or edit a JudoClass.


Solution

  • Your method takes one Student as parameter

    public Response createAccount(Student aStudent) {
    

    But you are sending an array.

    So your method should look like

    public Response createAccount(List<Student> aStudent) {