简体   繁体   English

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`java.time.LocalDate的实例

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

I'm facing an error while deserialize the string to object.将字符串反序列化为 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) 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/从 [Source: (String) 处的字符串值 ('2020-05-20') 反序列化的工厂方法

JSON JSON

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

    }

Model 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单元测试 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)参考无法构造`java.time.ZonedDateTime`的实例(没有创建者,如默认构造,存在)

Unable to Deserialize - Jackson LocalDate/Time - JUnit 无法反序列化 - Jackson 本地日期/时间 - JUnit

  1. Include jackson jsr310 module.包含 jackson jsr310 模块。
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.11.0</version>
</dependency>
  1. Register JavaTimeModule注册JavaTimeModule
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
  1. Mark the LocalDate type fields in your java class with following annotations.使用以下注释标记 java class 中的LocalDate类型字段。
@JsonFormat(pattern = "dd-MM-yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)

Complete code would be:完整的代码是:

Main class or junit:主class或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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 java 日期格式:com.fasterxml.jackson.databind.exc.InvalidDefinition.timeException:无法构造实例的`LocalDatejava. - in java date format: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`org.springframework.web.multipart.MultipartFile`的实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile` com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造“javax.Z0F635D0E0F3874FFF8B581C132E6C7A7B.bind.bind”的实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.xml.bind.JAXBElement` com.fasterxml.jackson.databind.exc.InvalidDefinitionException - com.fasterxml.jackson.databind.exc.InvalidDefinitionException com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到序列化程序 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException - java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException com.fasterxml.jackson.databind.exc.InvalidDefinitionException对于用户实体的某些REST操作 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException for some REST operations on User entity Quarkus com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到 class 的序列化程序 - Quarkus com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException - Java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM