日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 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);

  • 服務消費方定義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;

    }

    }

  • 服務消費方自定義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原因:主要是因為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(亲测)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。