简体   繁体   中英

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate

I'm facing an error while deserialize the string to object.

org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures) com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-05-20') at [Source: (String)

JSON

 {
       "studentId":57,
       "JoinedDate":"31-12-2019",
       "DOB":"08-06-1998"  

    }

Model

public class Student{

    private long studentId ;

    private LocalDate JoinedDate;

    private LocalDate DOB ;

    public long getStudentId() {
        return studentId;
    }

    public void setStudentId(long studentId) {
        this.studentId = studentId;
    }

    public LocalDate getJoinedDate() {
        return JoinedDate;
    }

    public void setJoinedDate(LocalDate joinedDate) {
        JoinedDate = joinedDate;
    }

    public LocalDate getDOB() {
        return DOB;
    }

    public void setDOB(LocalDate dOB) {
        DOB = dOB;
    }

Unit Test Class

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@WebAppConfiguration
public class StudentTest{


private Student student;
private ObjectMapper jsonObjectMapper;
@Before
public void setUp() throws IOException {

    jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
    studentJson = IOUtils.toString(getClass().getResourceAsStream(CommonTestConstants.StudentPath+ "/Student.json"));


student = jsonObjectMapper.readValue(studentJson , Student.class);
}

Any one please advise

Reference Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

Unable to Deserialize - Jackson LocalDate/Time - JUnit

  1. Include jackson jsr310 module.
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.11.0</version>
</dependency>
  1. Register JavaTimeModule
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
  1. Mark the LocalDate type fields in your java class with following annotations.
@JsonFormat(pattern = "dd-MM-yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)

Complete code would be:

Main class or junit:

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        Student student = objectMapper.readValue(YOUR_JSON_STRING, Student.class);
        System.out.println(student);
    }

Student:

public class Student {

    private long studentId;

    @JsonProperty("JoinedDate")
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate JoinedDate;

    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate DOB;

    // getters and setters and ToString
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM