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

问题描述 投票:0回答:1

将字符串反序列化为对象时遇到错误。

org.opentest4j.MultipleFailuresError:多次失败(2次失败) com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法 构造java.time.LocalDate的实例(没有创建者,如默认 构造,存在):无字符串参数构造函数/工厂方法 从[来源:(字符串)

的字符串值('2020-05-20')反序列化

JSON

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

    }

型号

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;
    }

单元测试类

@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);
}

任何人请指教

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

Unable to Deserialize - Jackson LocalDate/Time - JUnit

在将字符串反序列化为对象时遇到错误。 org.opentest4j.MultipleFailuresError:多次失败(两次失败)com.fasterxml.jackson.databind.exc.InvalidDefinitionException:...

java json junit jackson jackson-databind
1个回答
0
投票
  1. 包括杰克逊jsr310模块。
© www.soinside.com 2019 - 2024. All rights reserved.