0
votes

I'm currently building a REST API in Java. A request is based on a RequestBody and a JPA object.

Everything works fine when I send two form inputs for example:

{
    "date": "02-06-2021",
    "name": "test"
}

And here the JPA object:

package fm.bernardo.lb1.RequestBodies.Global;


import javax.persistence.Entity;
import javax.validation.constraints.NotEmpty;

@Entity
public class CountryCases {
    @NotEmpty (message = "You have to type in a data")
    private String date;

    @NotEmpty (message = "You have to type in a name")
    private String name;

    public CountryCases(String date, String name) {
        this.date = date;
        this.name = name;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return this.date;
    }

    public String getName() {
        return this.name;
    }
}

The data gets converted into a CountryCases object and is then accessible by the getters. Why do I get this error when sending only one form input:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of fm.bernardo.lb1.RequestBodies.Global.CountryCases (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of fm.bernardo.lb1.RequestBodies.Global.CountryCases (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 5]

Here the request data:

{
    "date": "02-06-2021"
}

And here the JPA object with a single attribute:

package fm.bernardo.lb1.RequestBodies.Global;


import javax.persistence.Entity;
import javax.validation.constraints.NotEmpty;

@Entity
public class CountryCases {
    @NotEmpty (message = "You have to type in a date")
    private String date;

    public CountryCases(String date) {
        this.date = date; 
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return this.date;
    }
}
1

1 Answers

0
votes

I just found out that this error is caused because there is no default (empty) constructor like this here:

package fm.bernardo.lb1.RequestBodies.Global;


import javax.persistence.Entity;
import javax.validation.constraints.NotEmpty;

@Entity
public class CountryCases {
    @NotEmpty (message = "You have to type in a date")
    private String date;

    public CountryCases() {}

    public CountryCases(String date) {
        this.date = date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return this.date;
    }
}