使用 Feign 调用分页接口报错:Method has too many Body parameters(亲测)

177 篇文章 6 订阅
50 篇文章 2 订阅

一、背景

  • 接口定义:
 

@ApiOperation(value = "分页查询会话")

@PostMapping(Routes.SESSIONS_QUERY)

JsonResult<Pagination<SessionInfo>> querySessions(@RequestBody @Valid SessionsQo qo,

@PageableDefault(size = 20, sort = "id", direction = Sort.Direction.DESC) Pageable pageable);

  • 服务消费方调用报错:
 

Method has too many Body parameters: public abstract com.xingren.common.data.JsonResult com.xingren.xxx.yyy.contract.api.controller.ISessionController.querySessions(com.xingren.xxx.yyy.contract.qo.SessionsQo,org.springframework.data.domain.Pageable)

二、解决

通言七墨过搜索、https://qimok.cn调研,目前有三种解决方法:

1、将分页属性直接通过入参传递,接口定义如下:

 

@ApiOperation(value = "分页查询会话")

@PostMapping(Routes.SESSIONS_QUERY)

JsonResult<Pagination<SessionInfo>> querySessions(@RequestBody @Valid SessionsQo qo,

@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("sort") Sort sort);

2、将分页对象冗余在Qo中(通过继承实现):

 

@Data

@NoArgsConstructor

@ApiModel(value = "查询会话")

public class SessionsQo extends PageableParam {

@ApiParam(value = "会话id列表")

private List<Long> sessionIdIn = Lists.newArrayList();

...

}

3、通过注解传递(参考:Issue):

  1. 服务提供方定义注解:
 

@Target(ElementType.PARAMETER)

@Retention(RetentionPolicy.RUNTIME)

public @interface PageableParam {

}

  1. 服务提供方定义接口:
 

@ApiOperation(value = "分页查询会话")

@PostMapping(Routes.SESSIONS_QUERY)

JsonResult<Pagination<SessionInfo>> querySessions(@RequestBody @Valid SessionsQo qo,

@PageableParam @SpringQueryMap Pageable pageable);

  1. 服务消费方定义processor
 

@Bean

public PageableParamProcessor pageableParamProcessor() {

return new PageableParamProcessor();

}

public static class PageableParamProcessor implements AnnotatedParameterProcessor {

private static final Class<PageableParam> ANNOTATION = PageableParam.class;

@Override

public Class<? extends Annotation> getAnnotationType() {

return ANNOTATION;

}

@Override

public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {

int parameterIndex = context.getParameterIndex();

MethodMetadata data = context.getMethodMetadata();

data.queryMapIndex(parameterIndex);

return true;

}

}

  1. 服务消费方自定义PageableUtil
 

public class PageableUtil extends PageRequest implements Map<String, Object> {

public static final String PAGE = "page";

public static final String SIZE = "size";

public static final String SORT = "sort";

@Delegate

protected Map<String, Object> delegate = Maps.newHashMap();

public PageableUtil(int page, int size, Sort sort) {

super(page, size, sort);

delegate.put(PAGE, page);

delegate.put(SIZE, size);

if (Objects.nonNull(sort)) {

delegate.put(SORT, sort.toString().replace(": ", ","));

}

}

public PageableUtil(int page, int size) {

super(page, size);

delegate.put(PAGE, page);

delegate.put(SIZE, size);

}

}

  • 定义PageableUtil原因:主要是因为FeignQueryMap类型参数的序列化和反序列化的言七墨方式与Sort.Order的不兼容,导致排序失效。
  1. 服务消费方调用方式:
 

SessionsQo qo = SessionsQo.builder().sessionIdIn(Collections.singletonList(20L)).build();

JsonResult<Pagination<SessionInfo>> pageInfo = sessionContract.querySessions(qo, new PageableUtil(0, 5, new Sort(Sort.Direction.DESC,

来源:使用 Feign 调用分页接口报错:Method has too many Body parameters | 七墨博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值