软件编程
位置:首页>> 软件编程>> java编程>> 通过FeignClient调用微服务提供的分页对象IPage报错的解决

通过FeignClient调用微服务提供的分页对象IPage报错的解决

作者:hu_xian_  发布时间:2022-01-27 20:19:23 

标签:FeignClient,微服务,分页对象,IPage

问题描述

通过FeignClient调用微服务提供的分页对象IPage报错

{
"message": "Type definition error: [simple type, class com.baomidou.mybatisplus.core.metadata.IPage]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.baomidou.mybatisplus.core.metadata.IPage` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 1, column: 37] (through reference chain: com.test.invoice.vo.ResponseVO[\"data\"])"
}

解决办法

当前mybatis-plus 本不支持 IPage,使用Page即可

生产者controller

通过FeignClient调用微服务提供的分页对象IPage报错的解决

Feign-api接口定义

通过FeignClient调用微服务提供的分页对象IPage报错的解决

消费者,Feign-api 调用

通过FeignClient调用微服务提供的分页对象IPage报错的解决

feign返回IPage无法返回结果集

通过feign调用,服务提供者返回IPage,客户端无法获取到结果;主要原因就是jackson需要默认构造函数序列化;

@PostMapping(value ="/page",consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseModel<IPage<CommentDTO>> loadCommentPage(@RequestBody Comment condition);

Mybatis-plus修改方式

IPage改成Page即可

spring-data 修改方式也是一样的,最简单就是自己实现IPage就可以了,也可以通过SpringMvcContract方式重造结果集

feign的几种可能性

1、接口直接被调用

2、接口直接被调用的同时其他模块也需要这个接口,那么其他模块就需要通过fegin调用

3、不会被直接调用,所有接口通过feign调用

基于以上三点,有如下可能性

1、在使用fegin时候,服务提供者到底应该直接返回ResponseModel(统一返回值)还是只返回结果?

1和3直接返回ResponseModel即可,2如果直接返回ResponseModel那么调用者就需要自己判断code或者直接返回给前段不做任何处理,如果直接返回结果那么直接被调用那个接口就无法统一返回值;建议直接写成俩个方法

来源:https://blog.csdn.net/hu_xian_/article/details/103961149

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com