wechatpay-apiv3 / wechatpay-java

微信支付 APIv3 的官方 Java Library
Apache License 2.0
961 stars 209 forks source link

java.lang.NoSuchMethodError: 'okhttp3.RequestBody #70

Closed Speefictor closed 1 year ago

Speefictor commented 1 year ago

错误描述

java.lang.NoSuchMethodError: 'okhttp3.RequestBody okhttp3.RequestBody.create(java.lang.String, okhttp3.MediaType)' at com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter.createOkHttpRequestBody(OkHttpClientAdapter.java:84) ~[wechatpay-java-core-0.2.0.jar:0.2.0] at com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter.buildOkHttpRequestBody(OkHttpClientAdapter.java:70) ~[wechatpay-java-core-0.2.0.jar:0.2.0] at com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter.buildOkHttpRequest(OkHttpClientAdapter.java:59) ~[wechatpay-java-core-0.2.0.jar:0.2.0] at com.wechat.pay.java.core.http.okhttp.OkHttpClientAdapter.innerExecute(OkHttpClientAdapter.java:46) ~[wechatpay-java-core-0.2.0.jar:0.2.0] at com.wechat.pay.java.core.http.AbstractHttpClient.execute(AbstractHttpClient.java:42) ~[wechatpay-java-core-0.2.0.jar:0.2.0] at com.wechat.pay.java.service.payments.jsapi.JsapiService.prepay(JsapiService.java:162) ~[wechatpay-java-0.2.0.jar:0.2.0]

重现bug的步骤

根据示例代码调用预下单接口

预期行为

完成下单

导致错误的代码片段

No response

操作系统

macOS

Java 版本

Java 17

wechatpay-java 版本

0.2.1

其他信息

No response

xy-peng commented 1 year ago

请参考下 #60 。

我猜测是你在项目中指定,或者其他组件依赖了 OkHttp 3.x。你可以手工指定 OkHttp 使用 4.x 版本。

implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))

另外,如果方便的话,能告诉我是什么引入了 OkHttp 3.x吗?或者贴一下完整依赖树。

Speefictor commented 1 year ago

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <java.version>17</java.version>
    <skipTests>true</skipTests>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.github.wechatpay-apiv3</groupId>
        <artifactId>wechatpay-java</artifactId>
        <version>0.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>4.34.68.ALL</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba.fastjson2</groupId>
        <artifactId>fastjson2</artifactId>
        <version>2.0.19.android</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.6.3</version>
        </plugin>
    </plugins>

</build>

xy-peng commented 1 year ago

我估计是因为你使用了 spring-boot 2.6,它依赖的是 OkHttp 3.14,所以导致 wechatpay-java-core 也用了 OkHttp 3.x。

现在有两条路:

Speefictor commented 1 year ago
com.github.wechatpay-apiv3 wechatpay-java 0.2.1 okhttp com.squareup.okhttp3 com.squareup.okhttp3 okhttp 5.0.0-alpha.10

谢谢,这个问题已解决,不过验证response又出错😅

keaixx365 commented 1 year ago

和楼主同样的问题 Spring-Boot 2.1.6 手工指定OkHttp 4.x 会报错java.lang.NoSuchMethodError: kotlin.collections.ArraysKt.copyInto([B[BIII)[B

Speefictor commented 1 year ago

@keaixx365 不升级spring-boot的方法如下: `

com.github.wechatpay-apiv3 wechatpay-java 0.2.1 okhttp com.squareup.okhttp3

com.squareup.okhttp3 okhttp 5.0.0-alpha.10 `
chengxu0808 commented 1 year ago

好歹是服务十几亿人的平台,多搞搞兼容性嘛