Petersoj / alpaca-java

A Java API for Alpaca, the commission free, algo friendly, stock trading broker. https://alpaca.markets
https://petersoj.github.io/alpaca-java/
MIT License
197 stars 82 forks source link

okhttp3 version issue with 8.2 release #108

Closed gregbarton closed 2 years ago

gregbarton commented 2 years ago

While attempting to execute orders I'm getting the following exception:

Exception in thread "Thread-6132" java.lang.NoSuchMethodError: 'okhttp3.RequestBody okhttp3.RequestBody.create(java.lang.String, okhttp3.MediaType)'
        at net.jacobpeterson.alpaca.util.okhttp.JSONBodyBuilder.build(JSONBodyBuilder.java:61)
        at net.jacobpeterson.alpaca.rest.endpoint.OrdersEndpoint.requestOrder(OrdersEndpoint.java:209)
        at net.jacobpeterson.alpaca.rest.endpoint.OrdersEndpoint.requestLimitOrder(OrdersEndpoint.java:281)

I notice that, despite there being a dependency on okhttp3 v4.9.1 the only version being downloaded during build is 3.14.9.

gregbarton commented 2 years ago

Placing an explicit dependency to version 4.9.1 in my maven build seems to have fixed the issue for now.

<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version>4.9.1</version>
</dependency>

But this is not an optimal solution.

Petersoj commented 2 years ago

This is likely due to transitive dependencies that Maven puts on the classpath. Another user opened an issue for the same reason here: #102. As far as I know, the only way to fix this with Maven is to explicitly specify the version of OkHttp you want to use by adding that dependency to your pom.xml as you have done. Another alternative is to use Gradle instead of Maven which features the implementation dependency clause which prevents putting transitive dependencies on the classpath.

Petersoj commented 2 years ago

@gregbarton were you able to resolve this issue?

gregbarton commented 2 years ago

Yeah, with the explicitly specified okhttp3 dependency. No issues since then.