You cannot instantiate an abstract class, Jackson neither. You should give Jackson information on how to instantiate MyAbstractClass with a concrete type.
See this answer on stackoverflow: Jackson JSON library: how to instantiate a class that contains abstract fields
And maybe also see Jackson Polymorphic Deserialization
Answer from jlabedo on Stack OverflowYou cannot instantiate an abstract class, Jackson neither. You should give Jackson information on how to instantiate MyAbstractClass with a concrete type.
See this answer on stackoverflow: Jackson JSON library: how to instantiate a class that contains abstract fields
And maybe also see Jackson Polymorphic Deserialization
I was having this issue using lombok's @Data and @Builder annotations alone, which I replaced with:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
And it solved my problem.
InvalidDefinitionException: Cannot construct instance
Serialization of Option: InvalidDefinitionException: Cannot construct instance of `scala.runtime.Nothing$
spring - Cannot solve databind.exc.InvalidDefinitionException: Cannot construct instance issue - Stack Overflow
rest - InvalidDefinitionException: Cannot construct instance of `com.vehicle.datatransferobject.VehicleDTO` - Stack Overflow
This exception :
InvalidDefinitionException: Cannot construct instance of com.vehicle.datatransferobject.VehicleDTO (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
means that Jackson didn't find a way to instantiate VehicleDTO that is the default constructor (no arg constructor) or a JsonCreator.
As you use a builder pattern you will configure the VehicleDTO class to make Jackson to instantiate VehicleDTO with the VehicleDTOBuilder such as :
@JsonDeserialize(builder = VehicleDTO.VehicleDTOBuilder.class)
public class VehicleDTO {
...
}
And annotate your builder with JsonPOJOBuilder as :
@JsonPOJOBuilder(buildMethodName = "createVehicleDTO", withPrefix = "set")
public static class VehicleDTOBuilder{
...
}
According to the javadoc, JsonPOJOBuilder is :
used to configure details of a Builder class: instances of which are used as Builders for deserialized POJO values, instead of POJOs being instantiated using constructors or factory methods. Note that this annotation is NOT used to define what is the Builder class for a POJO: rather, this is determined by JsonDeserialize.builder() property of JsonDeserialize.
I faced this error when I used Lombok's @Builder and @Data annotations together on a POJO class that is used for connecting to an API (either for consuming or for providing response)
I removed the @Builder annotation and then it is working fine