如何解决com.fasterxml.jackson.databind.exc.InvalidDefinitionException:使用龙目岛
在DTO中使用Lombok项目中的@Data
和@NoArgsConstructor
时发生奇怪的问题。
看来Jackson无法使用lombok注释对类进行序列化和反序列化。
我对Lombok并不陌生,但是我希望库会生成getter和setter,所以Jackson可以序列化和反序列化对象。
简短示例:
GetAllCustomersResponse.java
@Data
@NoArgsConstructor
public class GetAllCustomersResponse {
@JsonValue
private List<GetAllCustomersResponseElement> customers = new ArrayList<>();
public void addElement(GetAllCustomersResponseElement element) {
this.customers.add(element);
}
}
GetAllCustomersResponseElement.java
@Data
@NoArgsConstructor
public class GetAllCustomersResponseElement {
private Integer id;
private String name;
private Date birthdate;
}
当请求到达检索GetAllCustomersResponse
(包含所有客户的列表)时,在Jackson的序列化期间会发生以下异常:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.dto.GetAllCustomersResponseElement and no properties discovered to create BeanSerializer (to avoid exception,disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.dto.GetAllCustomersResponse["customers"]->java.util.ArrayList[0])
AFAIK,“我们找不到要序列化的类” 通常会在我们要序列化的类中不存在吸气剂和塞特剂时发生。
如果我写了所有的“样板”吸气剂和二传手,杰克逊可以正常工作。
但是,如果我使用的是龙目岛的@Data
,为什么杰克逊没有可用的吸气剂和吸气剂?
来自Lombok docs:
@数据
现在全部在一起:
@ToString
,@EqualsAndHashCode
, 在所有字段上@Getter
,在所有非最终字段上@Setter
,以及@RequiredArgsConstructor
!
我想念什么吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。