java - 将对象转换为 JSON 字符串时发生 JsonMappingException - org.apache.avro.AvroRuntimeException : Not an array:

标签 java json jackson json-deserialization objectmapper

在将 Java 对象转换为 JSON 字符串时,我遇到了 JsonMappingException。以下是完整的异常消息。

com.fasterxml.jackson.databind.JsonMappingException: Not an array: {"type":"record","name":"ClearingSystemMemberIdentification2","namespace":"com.sam.eps.paymentdomain.avro.pacs002","doc":"Schema for com.sam.eps.iso.pacs002.ClearingSystemMemberIdentification2","fields":[{"name":"clearing_system_identification","type":["null",{"type":"record","name":"ClearingSystemIdentification2Choice","doc":"Schema for com.sam.eps.iso.pacs002.ClearingSystemIdentification2Choice","fields":[{"name":"code","type":["null",{"type":"string","avro.java.string":"String"}],"default":null},{"name":"proprietary","type":["null",{"type":"string","avro.java.string":"String"}],"default":null}]}],"default":null},{"name":"member_identification","type":["null",{"type":"string","avro.java.string":"String"}],"default":null}]} (through reference chain: com.sam.eps.paymentdomain.avro.pacs002.Document["fi_to_fi_payment_status_report"]->com.sam.eps.paymentdomain.avro.pacs002.FIToFIPaymentStatusReportV10["group_header"]->com.sam.eps.paymentdomain.avro.pacs002.GroupHeader91["instructed_agent"]->com.sam.eps.paymentdomain.avro.pacs002.BranchAndFinancialInstitutionIdentification6["financial_institution_identification"]->com.sam.eps.paymentdomain.avro.pacs002.FinancialInstitutionIdentification18["clearing_system_member_identification"]->com.sam.eps.paymentdomain.avro.pacs002.ClearingSystemMemberIdentification2["schema"]->org.apache.avro.Schema$RecordSchema["elementType"])
下面是我用于将 Java 对象转换为 JSON 字符串的 Java 代码。我尝试启用 ACCEPT_SINGLE_VALUE_AS_ARRAY、ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT 但仍然面临问题。不知道是什么原因引起的 JsonMappingException: 不是数组 问题。
private String convertObjectToJson(Object request) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
            @Override
            public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime));
            }
        });
        simpleModule.addSerializer(LocalDate.class, new JsonSerializer<LocalDate>() {
            @Override
            public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString(DateTimeFormatter.ISO_LOCAL_DATE.format(localDate));
            }
        });
        objectMapper.registerModule(simpleModule);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper.writeValueAsString(request);
    }

最佳答案

Jackson 转换过程递归地评估对象的每个字段或 getter,因此它可以创建 JSON 节点。
您可能正在此处转换生成的 AVRO 类型。它包含一个名为 $SCHEMA 的字段它本身没有名为 getElementType() 的方法实现的。默认方法getElementType()org.apache.avro.Schema显式抛出此异常。由于 Jackson 在转换过程中遇到了这个方法,所以抛出异常。getElementType()只是该 Schema 类中默认抛出异常的许多默认实现之一。我不认为有任何解决方法,除了不使用 Jackson 创建 JSON,而是使用最初创建 AVRO 类型的相同 API。
编辑:也许你可以注册一个 JsonSerializer<Schema>到您的映射器,并自己自定义 Schema 的序列化。

关于java - 将对象转换为 JSON 字符串时发生 JsonMappingException - org.apache.avro.AvroRuntimeException : Not an array:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62944201/

相关文章:

json - 即使所有字段都为空,@JsonUnwrapped 也会创建非空对象

java - 406 尝试解析从 Spring-MVC Controller 返回的 json 时出错

java - 使用补间动画缩小尺寸后,imageview 单击监听器仍然有效

java - 如何将 Mat (opencv) 转换为 INDArray (DL4J)?

python - 如何在Python中正确错误检查JSON值?

java - 为 post 请求创建 json

java - Action 监听器中实例化按钮的 NullPointerException

java - 将 Java 嵌入到 C++ 应用程序中?

java - 如何包装 OAuth2 异常?

c# - 在 JSON.NET 中如何获取对每个反序列化对象的引用?