[英]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
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.11.0</version>
</dependency>
JavaTimeModule
JavaTimeModule
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
LocalDate
type fields in your java class with following annotations.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.