I'm referring this tutorial to learn the basics of Jackson, but badly tumbled into this.
This is the exception:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `hello.Car` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"color":"Black","type":"BMW"}"; line: 1, column: 2]
This is the class:
@Builder
@Data
@AllArgsConstructor
public class Car implements Serializable {
private String color;
private String type;
}
And this is the parsing logic:
@Service
public class HelloService {
public void testJackson() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String json1 = "{\"color\":\"Black\",\"type\":\"BMW\"}";
Car car1 = objectMapper.readValue(json1, Car.class);
}
}
F.Y.I I'm using Jackson and Lombok:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
What's really the matter here. Please share some ideas.
UPDATE 2018-05-10
@Hemant Patel, thanks. Just added the @NoArgsConstructor
, like a magic. It works as expected. I will look into it to find out what's really going on.
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car implements Serializable {
String color;
String type;
}
B.T.W Thanks @Thomas and @Alex, I checked these two posts but I really don't think the real cause lies in the relationship with Lombok.