【发布时间】:2020-09-27 06:53:32
【问题描述】:
将字符串反序列化为对象时遇到错误。
org.opentest4j.MultipleFailuresError:多次失败(2次失败) com.fasterxml.jackson.databind.exc.InvalidDefinitionException:不能 构造
处的字符串值 ('2020-05-20') 反序列化java.time.LocalDate的实例(没有创建者,默认 构造,存在):没有字符串参数构造函数/工厂方法 从 [Source: (String)
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)
【问题讨论】:
-
请使用您将 json 反序列化为 pojo 的代码更新问题。
-
@Smile - 我已经更新了映射部分的问题
标签: java json junit jackson jackson-databind