【问题标题】:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDatecom.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`java.time.LocalDate的实例
【发布时间】:2020-09-27 06:53:32
【问题描述】:

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

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

处的字符串值 ('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

【问题讨论】:

  • 请使用您将 json 反序列化为 pojo 的代码更新问题。
  • @Smile - 我已经更新了映射部分的问题

标签: java json junit jackson jackson-databind


【解决方案1】:
  1. 包含 jackson jsr310 模块。
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.11.0</version>
</dependency>
  1. 注册JavaTimeModule
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
  1. 使用以下注释标记您的 java 类中的 LocalDate 类型字段。
@JsonFormat(pattern = "dd-MM-yyyy")
@JsonDeserialize(using = LocalDateDeserializer.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);
    }

学生:

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
}

【讨论】:

  • 非常感谢@Smile - 我尝试了你的建议 - 我收到一个错误 - org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures) com.fasterxml.jackson.databind.exc.InvalidFormatException :无法从字符串“2020-05-20”反序列化 java.time.LocalDate 类型的值:无法反序列化 java.time.LocalDate:(java.time.format.DateTimeParseException)文本“2020-05-20”无法解析[Source: (String)" 处的索引 2 可以请您说一下吗?
  • 看起来您正在尝试使用日期格式与所提到的格式不同的数据 - dd-MM-yyyy。您的 json 中没有您期望的任何固定日期格式吗?
  • 感谢您快速转身 - 我传入 json 的格式是正确的 31-02-2019。但我猜 LocalDate 格式不接受 dd-MM-yyyy - 它需要默认共振峰 - 异常我得到 org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures) java.time.format.DateTimeParseException: Text '2019-12- 31' 无法在索引 4 处解析
  • 我希望你在Student类的注解中使用LocalDateDeserializer而不是LocalDateTimeDeserializer
  • 查看此答案以获取 LocalDate stackoverflow.com/a/51546069/1776132 的全局配置
猜你喜欢
  • 2020-03-08
  • 2020-05-07
  • 2020-04-21
  • 2018-07-29
  • 2020-01-01
  • 2019-10-16
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多