JSON parse error: Cannot deserialize instance of ArrayList

Tags: , , , ,



I’m trying to send a JSON to a POST bodyRequest service to use this information as a Java ArrayList or something like that. When I try to do it, I receive a deserialize error

This is my JSON:

{information: [ {fields: “Periods Offered”, tables: “Courses”, columns: “Academic Level*”}, {fields: “Default Grading Basis*”, tables: “Courses”, columns: “Default Offering Percentage”}, {fields: “Allowed Locations”, tables: “Courses”, columns: “Allowed to Offer”} ] }

This is my POST bodyRequest service

@ResponseBody
@PostMapping("/dataMapping/update/table")

public ResponseEntity<Object> updateDataMappingTable(@RequestBody List<UpdateDataMapping> information) {

    try {
        String update = this.dataMappingService.update(information);
        return new ResponseEntity<Object>(update, HttpStatus.OK);
    } catch (Exception e) {
        Response response = new Response();
        response.setError(e.getMessage());
        return new ResponseEntity<Object>(response, HttpStatus.OK);
    }
}

My UpdateDataMapping class looks like:

public class UpdateDataMapping {

    public String fields;
    public String tables;
    public String columns;

    public UpdateDataMapping() {

    }

    public UpdateDataMapping(String fields, String tables, String columns) {
    
        this.fields = fields;
        this.tables = tables;
        this.columns = columns;
    }

    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    public String getTables() {
        return tables;
    }

    public void setTables(String tables) {
        this.tables = tables;
    }

    public String getColumns() {
        return columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }
}

This is my error:

.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.ArrayList<com.kastech.model.UpdateDataMapping> out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<com.kastech.model.UpdateDataMapping> out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]]

If someone can help me I will appreciate it.

Answer

Your JSON example shows that your array is delivered inside an information object. You are seeing your current stack trace because the Jackson is unable to find an appropriate way to deserialize your request payload.

Either, you can change your request JSON to simply be a list

[ {fields: "Periods Offered", tables: "Courses", columns: "Academic Level*"}, {fields: "Default Grading Basis*", tables: "Courses", columns: "Default Offering Percentage"}, {fields: "Allowed Locations", tables: "Courses", columns: "Allowed to Offer"} ]

or you can have a POJO represent that information object

class UpdateMappingList {
    List<UpdateDataMapping> information;
    ...
}


Source: stackoverflow