Spring Boot JSON parse error: Cannot construct instance of model

user15296624

I am catching error:

Resolved [org.springframework.http.converter.
HttpMessageNotReadableException: JSON parse error: Cannot 
construct instance of `model.Record` (although at least one Creator exists): no 
int/Int-argument constructor/factory method to deserialize from Number value (2); 
nested exception is com.fasterxml.jackson.databind.exc.
MismatchedInputException: Cannot construct instance of 
`model.Record` (although at least
 one Creator exists): no int/Int-argumentconstructor/factory
 method to deserialize from Number value (2)
 at [Source: (PushbackInputStream); line: 1, column: 123] (through reference chain: 
model.CreateUserRequestDTO["records"]->java.util.ArrayList[0])]

I have 2 entities: User and Record.

public class Developer{
    private long id;
    private String name;
    private List<Record> records;
}

Second class:

@Data
public class Record{
    private long id;
    private String title;
}

In my front end I have a records select with multiple options, where value is id and title - I'm displaying as options.

When I try to send my request to server, request looks like:

{
  "name": "Ivan",
  "records": [1, 3, 7] // selected options
}

How can I fix it? Shouldn't Spring Boot take id and make entities from them?

YCF_L

Your JSon is not correct, it doesn't match with your class, it should look like this:

{
  "name": "Ivan",
  "records": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    }
  ]
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

java.lang.Object; cannot be cast to model class: error in Spring Boot

JSON parse error: Can not construct instance of class

Error of "Cannot deserialize" in Spring Boot

Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

JSON parse error: Cannot deserialize instance

Cannot construct instance of - Jackson

Json Parse Error - MismatchedInputException expecting array or string for LocalDate in Spring Boot

JSON parse error: Cannot deserialize instance of ArrayList

How to Fix This ERROR "JSON parse error: Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT

JSON parse error: Cannot construct instance of `com.example.greetapp.domain.User`

How to parse json in Spring Boot and write to a repository

Model cannot be cast - internal error in spring boot and Criteria API Join

Spring Boot web cannot parse nested json to object

spring boot mongodb json parse string as a date error

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance

Spring Boot 2 / GraphQL Extended Scalars not working: Exception "Cannot construct instance" "cannot deserialize"

how to parse some field of json spring boot

JSON parse error: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists)

Spring Boot - Jersey Client - Jackson Cannot construct instance of `java.time.Instant`

Unable to parse JSON in spring boot

Failed to construct transformer: Error: Cannot parse C:

Could not read JSON: Cannot construct instance of `java.util.LinkedHashMap`

JSON parse error: Can not deserialize instance - Java

Cannot load configuration class error spring boot

Many-To-One : JSON parse error Cannot construct instance

JSON Parse error when submitting AJAX post to Spring Boot Controller using Thymeleaf

Spring controller cannot parse JSON sent from HttpClient (Cannot construct instance of `java.util.LinkedHashMap`)

Spring DTO: Cannot construct instance of <DTO interface>

Cannot parse constant error in SQL and Spring Boot