Skip to content

fix: Fix the NoSuchMethodError error that occurs when calling a post request without a request body when your project depends on OkHttp3.x #373

@hanpijun-buhanpi

Description

@hanpijun-buhanpi

错误描述

当项目使用OkHttp3.x时调用了无需请求体的POST请求时,将会抛出NoSuchMethodError异常

重现bug的步骤

  1. 构建配置
    RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder().build();
  2. 构建服务
    GiftActivityService service = new GiftActivityService.Builder().config(config).build();
  3. 新建请求
    TerminateActivityRequest request = new TerminateActivityRequest();
  4. 设置参数
    request.setActivityId("1");
  5. 发起请求
    TerminateActResponse response = service.terminateActivity(request);

预期行为

我期望它不会报错

导致错误的代码片段

public final class OkHttpClientAdapter extends AbstractHttpClient {
  // ...
  private RequestBody buildOkHttpRequestBody(
      com.wechat.pay.java.core.http.RequestBody wechatPayRequestBody) {
    if (wechatPayRequestBody == null) {
      // create an empty request body
      return RequestBody.create("", null); // 报错
    }
    // ...
  }
  //...
}

操作系统

Windows11

Java 版本

Java 17

wechatpay-java 版本

v0.2.17

其他信息

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions