本人今日进行springcloud微服务搭建时出现报错,feign客户端远程调用的数据回调无法转换为我要的Result类
报错代码:2024-05-30 16:37:27.746 ERROR 18056 --- [nio-6703-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.codec.DecodeException: Type definition error: [simple type, class com.beauty.entity.Result]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.beauty.entity.Result` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.beauty.entity.Result` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
用百度翻译了一下
报错代码:2024-05-30 16:37:27.746错误18056-[nio-6703-exec-1]o.a.c.c.[.[./]。[dispatcherServlet]:路径为[]的上下文中Servlet[dispatcherCervlet]的Servlet.service()引发异常[请求处理失败;嵌套异常为feign.codec.DecodeException:类型定义错误:[simple Type,class com.beauty.entity.Result];嵌套异常为com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`com.beauty.entity的实例。结果`(不存在创建者,如默认构造函数):无法从Object值反序列化(没有基于委托或属性的创建者)
位于[Source:(PushbackInputStream);行:1,列:2]],具有根本原因
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`com.beauty.entity的实例。结果`(不存在创建者,如默认构造函数):无法从Object值反序列化(没有基于委托或属性的创建者)
在[源:(PushbackInputStream);行:1,列:2]
这句话提醒了我,随即对项目中的pojo类Result进行添加无参构造函数便成功解决了问题
成功解决:
tips:希望以后大家可以记得写类的时候记得写默认的无参构造函数,即使看着他没用,但是也会有意想不到的报错。这次的报错原因我猜是我调用了个无参的controller方法刚好需要一个无参的Rusult?但是原本没有所以报错了