使用 Feign 调用分页接口报错:Method has too many Body parameters(亲测)
一、背景
- 接口定義:
| @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):
| @Target(ElementType.PARAMETER) | |
| @Retention(RetentionPolicy.RUNTIME) | |
| public @interface PageableParam { | |
| } |
| @ApiOperation(value = "分頁查詢會話") | |
| @PostMapping(Routes.SESSIONS_QUERY) | |
| JsonResult<Pagination<SessionInfo>> querySessions(@RequestBody @Valid SessionsQo qo, | |
| @PageableParam @SpringQueryMap Pageable pageable); |
| @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; | |
| } | |
| } |
| 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原因:主要是因為Feign對QueryMap類型參數的序列化和反序列化的言七墨方式與Sort.Order的不兼容,導致排序失效。
| 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 | 七墨博客
總結
以上是生活随笔為你收集整理的使用 Feign 调用分页接口报错:Method has too many Body parameters(亲测)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 山姆全城配送用什么车有用面包车的吗?
- 下一篇: 蛇头是什么车?