无法构造'com.baomidou.mybatisplus.core.metadata.ipage'的实例

2019年08月15日 16:44 · 阅读(14625) ·

[目录]

参考

通过feignClient调用微服务提供的分页对象IPage<T>报错

相关项目

IntelliJ IDEA 2018.3 创建 Maven 多模块(Module)项目

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0
mybatis-plus-boot-starter 3.1.0

问题描述

错误提示

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

  1. {
  2. "timestamp": "2019-08-15T08:25:03.323+0000",
  3. "status": 500,
  4. "error": "Internal Server Error",
  5. "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\"])",
  6. "path": "/enta/api/Inv/Api/Test/Query"
  7. }

相关代码

1.生产者

  1. @Override
  2. public ResponseVO<IPage<TRbtTestData>> Query(@RequestBody SuperEntity param){
  3. Page<TRbtTestData> page = new Page<>();
  4. page.setCurrent(1);
  5. page.setSize(10);
  6. return new ResponseVO<>(ResponseCode.OK, baseMapper.selectPageVO(page, param));
  7. IPage<TRbtTestData> ipageData = baseMapper.selectPageVO(page, param);
  8. }

2.Feign-api接口定义

  1. @FeignClient(value = "service-producer")
  2. public interface TRbtTestConsumer {
  3. @PostMapping("/Test/Add")
  4. ResponseVO<String> Add(@RequestBody TRbtTestData data);
  5. @PostMapping("Test/Update")
  6. ResponseVO<Boolean> Update(@RequestBody TRbtTestData data);
  7. @RequestMapping(value="Test/Del",method=RequestMethod.GET)
  8. ResponseVO<Boolean> Del(@RequestParam(value="name",required = true) String name,@RequestParam(value="version",required = true) String version);
  9. @PostMapping("/Test/Get")
  10. ResponseVO<TRbtTestData> Get(@RequestBody TRbtTestData data);
  11. @PostMapping("/Test/Query")
  12. ResponseVO<IPage<TRbtTestData>> Query(@RequestBody SuperEntity param);
  13. }

3.消费者,Feign-api 调用

  1. @PostMapping("/Test/Query")
  2. @ApiOperation(value = "系统框架测试-获取分页数据", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-获取分页数据")
  3. public ResponseVO Query(@RequestBody SuperEntity param){
  4. return testConsumer.Query(param);
  5. }

原因分析

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

问题解决

1.生产者

  1. @Override
  2. public ResponseVO<Page<TRbtTestData>> Query(@RequestBody SuperEntity param){
  3. Page<TRbtTestData> page = new Page<>();
  4. page.setCurrent(1);
  5. page.setSize(10);
  6. //return new ResponseVO<>(ResponseCode.OK, baseMapper.selectPageVO(page, param));
  7. IPage<TRbtTestData> ipageData = baseMapper.selectPageVO(page, param);
  8. //IPage<TRbtTestData> 转换为 Page<TRbtTestData>
  9. Page<TRbtTestData> pageData = new Page<>();
  10. pageData.setRecords(ipageData.getRecords());
  11. pageData.setCurrent(ipageData.getCurrent());
  12. pageData.setSize(ipageData.getSize());
  13. pageData.setTotal(ipageData.getTotal());
  14. return new ResponseVO<>(ResponseCode.OK, pageData);
  15. }

2.Feign-api接口定义

  1. @FeignClient(value = "service-producer")
  2. public interface TRbtTestConsumer {
  3. @PostMapping("/Test/Add")
  4. ResponseVO<String> Add(@RequestBody TRbtTestData data);
  5. @PostMapping("Test/Update")
  6. ResponseVO<Boolean> Update(@RequestBody TRbtTestData data);
  7. @RequestMapping(value="Test/Del",method=RequestMethod.GET)
  8. ResponseVO<Boolean> Del(@RequestParam(value="name",required = true) String name,@RequestParam(value="version",required = true) String version);
  9. @PostMapping("/Test/Get")
  10. ResponseVO<TRbtTestData> Get(@RequestBody TRbtTestData data);
  11. @PostMapping("/Test/Query")
  12. ResponseVO<Page<TRbtTestData>> Query(@RequestBody SuperEntity param);
  13. }

3.消费者,Feign-api 调用

  1. @PostMapping("/Test/Query")
  2. @ApiOperation(value = "系统框架测试-获取分页数据", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-获取分页数据")
  3. public ResponseVO Query(@RequestBody SuperEntity param){
  4. return testConsumer.Query(param);
  5. }

测试结果