如何解决objectMapper 无法反序列化外部定义的 pojo 类
在我的项目中,我有一个 common-lib,它在所有服务之间共享,我在上面定义了
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Builder
@Data
public class Error {
private String errorId;
private String message;
}
在另一个以 common-lib 作为依赖项的服务上,我尝试将字符串反序列化为这个 Error
public static void main(String[] args) throws JsonProcessingException {
String input = "{\n" +
" \"errorId\": \"errorId\",\n" +
" \"message\": \"User not found\"\n" +
"}";
ObjectMapper objectMapper = new ObjectMapper().registerModules(
new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)
);
System.out.println(objectMapper.readValue(input,xxx.xxx.common.Error.class));
}
它失败了
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `xxx.xxx.xxx.Error` (no Creators,like default constructor,exist): cannot deserialize from Object value (no delegate- or property-based Creator)
但是,当我将生成的 Error 类从 common-lib 复制到我的服务中时,它按预期工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。