英文:
Jackson unable to recognise base class fields
问题
I have translated the code snippet for you:
我有以下的父类
```java
@Builder
@Jacksonized
@EqualsAndHashCode
@ToString
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes({
@JsonSubTypes.Type(value = Child.class, name = "Child")
}
)
public class Parent implements Serializable {
private static final long serialVersionUID = 6223930820946596247L;
@JsonProperty("recipient")
protected Recipient recipient;
.....
}
子类如下
@Builder(builderMethodName = "childRequestBuilder")
@Jacksonized
@EqualsAndHashCode
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Child extends Parent implements Serializable {
private static final long serialVersionUID = -2848064640409441165L;
@JsonProperty("use_case")
private String useCase;
}
我正在尝试运行以下测试以检查反序列化。但是,这会引发以下错误
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
未识别的字段 "recipient"(类 a.b.Child$ChildRequestBuilder),
未标记为可忽略的字段(已知属性: "use_case")在 [Source:
(String) "{"recipient":{"endpoint_details":{"someToken":"someToken","someDevice":"someDevice","someApp":"someApp"}}}";
行: 1, 列: 129](通过引用链:
a.b.Child$ChildRequestBuilder["recipient"])
@Test
public void testChildDeserialization() throws JsonProcessingException {
SomeEndPoint endPoint = SomeEndpoint.builder().someToken("someToken").someDevice("someDevice").someApp("someApp").build();
Recipient recipient = Recipient.builder().endpointDetails(endPoint).build();
Child child = Child.childRequestBuilder().build();
child.setRecipient(recipient);
String request = new ObjectMapper().writeValueAsString(child);
Child deserializedChild = new ObjectMapper().readValue(request, Child.class);
SomeEndPoint someEndPoint = (SomeEndPoint) deserializedChild.getRecipient().getEndpointDetails();
Assert.assertEquals(someEndPoint.getSomeToken(), endPoint.getSomeToken());
}
希望这对您有所帮助。
英文:
I have the following Parent class
@Builder
@Jacksonized
@EqualsAndHashCode
@ToString
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
@NoArgsConstructor
@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes({
@JsonSubTypes.Type(value = Child.class, name = "Child")
}
)
public class Parent implements Serializable {
private static final long serialVersionUID = 6223930820946596247L;
@JsonProperty("recipient")
protected Recipient recipient;
.....
}
The Child class is as follows
@Builder(builderMethodName = "childRequestBuilder")
@Jacksonized
@EqualsAndHashCode
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Child extends Parent implements Serializable {
private static final long serialVersionUID = -2848064640409441165L;
@JsonProperty("use_case")
private String useCase;
}
I am trying to run the following test to check deserialisation. However, this throws the following error
> com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
> Unrecognized field "recipient" (class a.b.Child$ChildRequestBuilder),
> not marked as ignorable (1 known properties: "use_case"]) at [Source:
> (String)"{"recipient":{"endpoint_details":{"someToken":"someToken","someDevice":"someDevice","someApp":"someApp"}}}";
> line: 1, column: 129] (through reference chain:
> a.b.Child$ChildRequestBuilder["recipient"])
@Test
public void testChildDeserialization() throws JsonProcessingException {
SomeEndPoint endPoint = SomeEndpoint.builder().someToken("someToken").someDevice("someDevice").someApp("someApp").build();
Recipient recipient = Recipient.builder().endpointDetails(endPoint).build();
Child child = Child.childRequestBuilder().build();
child.setRecipient(recipient);
String request = new ObjectMapper().writeValueAsString(child);
Child deserialisedChild = new ObjectMapper().readValue(request, Child.class);
SomeEndPoint someEndPoint = (SomeEndPoint) deserialisedChild.getRecipient().getEndpointDetails();
Assert.assertEquals(someEndPoint.getSomeToken(), endPoint.getSomeToken());
}
答案1
得分: 0
我发现 Lombok 注解 @Jacksonized 是罪魁祸首。移除它解决了问题。简单来说,@Jacksonized 干扰了 Jackson 反序列化的方式,通过覆盖一些东西。它会自动配置生成的构建器类供 Jackson 反序列化使用。
英文:
I figured that the Lombok annotation @Jacksonized was the culprit here. Removing it solved the issue. To over-simplify, @Jacksonized interferes with how Jackson does deserialization by overriding a few things. It automatically configures the generated builder class to be used by Jackson's deserialization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论