Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Speefictor opened this issue Nov 30, 2022 · 7 comments · Fixed by #76
Closed

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

Speefictor opened this issue Nov 30, 2022 · 7 comments · Fixed by #76
Labels
bug Something isn't working

Comments

@Speefictor
Copy link

错误描述

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

@Speefictor Speefictor added the bug Something isn't working label Nov 30, 2022
@xy-peng
Copy link
Contributor

xy-peng commented Nov 30, 2022

请参考下 #60

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

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

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

@Speefictor
Copy link
Author


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.6.3



<java.version>17</java.version>
true



org.springframework.boot
spring-boot-starter-data-jpa


org.springframework.boot
spring-boot-starter-data-rest

	<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
Copy link
Contributor

xy-peng commented Nov 30, 2022

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

现在有两条路:

  • 手工指定 OkHttp 使用 4.x。但是需要观察 spring-boot 对 OkHttp 4.x 的兼容性。虽然 OkHttp4 号称”除了一些小的例外(如下),OkHttp 4.x 与 OkHttp 3.x 二进制和 Java 源代码兼容“。
  • 将 spring-boot 框架升级到 2.7。在 2.7 后使用的是 OkHttp 4.x。这样可以避免 spring-boot 对 OkHttp4 的兼容问题,你需要付出框架的迁移成本。

@Speefictor
Copy link
Author

com.github.wechatpay-apiv3 wechatpay-java 0.2.1 okhttp com.squareup.okhttp3 com.squareup.okhttp3 okhttp 5.0.0-alpha.10

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

@keaixx365
Copy link

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

@Speefictor
Copy link
Author

Speefictor commented Nov 30, 2022

@keaixx365 不升级spring-boot的方法如下:
<dependency> <groupId>com.github.wechatpay-apiv3</groupId> <artifactId>wechatpay-java</artifactId> <version>0.2.1</version> <exclusions> <exclusion> <artifactId>okhttp</artifactId> <groupId>com.squareup.okhttp3</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>5.0.0-alpha.10</version> </dependency>

@xy-peng xy-peng closed this as completed Dec 5, 2022
@chengxu0808
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants