Jackson JSON在反序列化集合时阻塞

j2cgzkjk  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(148)

Java 11,Spring Boot 2.5和Jackson 2.12。我有以下Java类:

// using lombok annos to generate getters & setters
@Data
public class ExamUnit implements Comparable<ExamUnit> {

    private Long id;
    private String name;
    private String displayName;
    private Long order;
    private Exam exam;

    // compareTo, equals & hashCode are necessary so that the TreeSet below sorts ExamUnits
    // correctly, according to my needs
    @Override
    public int compareTo(ExamUnit other) {
        if (this.equals(other)) {
            return 0;
        } else if (this.order < other.order) {
            return 1;
        }

        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ExamUnit examUnit = (ExamUnit) o;
        return Objects.equals(name, examUnit.name) &&
                Objects.equals(displayName, examUnit.displayName) &&
                Objects.equals(order, examUnit.order);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, displayName, order);
    }
}

以及:

@Data
public class Exam {

    private Long id;
    private String name;
    private String displayName;
    private SortedSet<ExamUnit> units = new TreeSet<>();

}

然后我有一个“exam”JSON字符串:

{
  "id": 1,
  "name": "science",
  "displayName": "Science Exam",
  "units": [
    {
      "id": 1,
      "name": "chemistry",
      "displayName": "Chemistry Unit",
      "order": 1
    },
    {
      "id": 2,
      "name": "biology",
      "displayName": "Biology Unit",
      "order": 2
    }
  ]
}

这是一个有效的JSON,您可以自己验证,而且它看起来确实正确地遵循了Java类型的模型。
然后,我有一些代码来读取字符串并将其反序列化:

@Test
public void deserializeExamJson() {

    String examJson = "{\n" +
            "  \"id\": 1,\n" +
            "  \"name\": \"science\",\n" +
            "  \"displayName\": \"Science Exam\",\n" +
            "  \"units\": [\n" +
            "    {\n" +
            "      \"id\": 1,\n" +
            "      \"name\": \"chemistry\",\n" +
            "      \"displayName\": \"Chemistry Unit\",\n" +
            "      \"order\": 1\n" +
            "    },\n" +
            "    {\n" +
            "      \"id\": 2,\n" +
            "      \"name\": \"biology\",\n" +
            "      \"displayName\": \"Biology Unit\",\n" +
            "      \"order\": 2\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    Exam exam = null;
    try {
        exam = objectMapper.readValue(examJson, Exam.class);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }

    assertThat(exam.getName()).isEqualTo("science");

}

当此测试运行时,它将失败,原因是:

java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.util.SortedSet)
 at [Source: (StringReader); line: 6, column: 5] (through reference chain: com.me.myapp.Exam["units"])
ar5n3qh5

ar5n3qh51#

在ExamUnit类中,将属性private Exam exam;标注为@JsonIgnore

相关问题