I have a Spring Boot application using version 2.2.4 and Zulu Java 11.0.5 from Azul. It is accessing a REST web service which is deployed on a Payara web server (version 5.194).
I am using the following DTOs:
public class IdDTO extends BasicResponseDTO {
private long id;
public IdDTO(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
and
public class BasicResponseDTO implements Serializable {
private String errorCode;
public BasicResponseDTO() {
this.setErrorCode(null);
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
I invoke a REST web service and from Postman I see I receive (correctly) the following response:
{
"errorCode": null,
"id": 3534016
}
However, when I retrieve the response, I get the following exception:
class org.springframework.web.client.RestClientException/Error while extracting response for type [class com.dto.IdDTO] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.dto.IdDTO` (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 `com.dto.IdDTO` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
Does anyone have any idea on why the application is not able to map the received JSON to the object?
P.S. 1) I also have other DTOs that extend BasicResponseDTO and the de-serialization works fine for them.
P.S. 2) The definition of the classes is the same on both the server and the client.
3. JsonMappingException: No Suitable Constructor 3.1. The Problem Now let's look at the common JsonMappingException: No Suitable Constructor found for type. This exception is thrown if Jackson can't access the constructor. In the following example, class User doesn't have a default constructor:
This exception is thrown if there is an unknown property in the JSON String while deserializing. We'll try to deserialize a JSON String with extra property “ checked “:
This exception is thrown if the JSON doesn't match exactly what Jackson is looking for. For example, the main JSON could be wrapped: 4.2. The Solution We can solve this problem using the annotation @JsonRootName:
Finally, let's quickly discuss the Jackson “No such method” errors. When java.lang.NoSuchMethodError Exception is thrown, it's usually because we have multiple (and incompatible) versions of Jackson jars on our classpath.
There is no default constructor on IdDTO. Only one that takes id:
public IdDTO(long id) {
this.id = id;
}
You have to add one:
public IdDTO() {
}
This is needed by JSON deserialization to construct objects from your classes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With