commercetools / commercetools-sdk-java-v2

The e-commerce SDK from commercetools for Java.
https://commercetools.github.io/commercetools-sdk-java-v2/javadoc/index.html
Apache License 2.0
35 stars 16 forks source link

java.lang.NoSuchMethodError: okhttp3.RequestBody.create([BLokhttp3/MediaType;)Lokhttp3/RequestBody; error #106

Closed georgemarin closed 3 years ago

georgemarin commented 3 years ago

Describe the bug Hello, I am trying to send a request using the Java SDK v2. any requests that I send throws exception

To Reproduce Steps to reproduce the behavior:

  1. Perform any operation

Screenshots If applicable, add screenshots to help explain your problem. image

Desktop (please complete the following information):

Additional context My code is pretty basic, just copy-paste from the docs. Please see below:

` ApiRoot apiRoot = ApiFactory.create( ClientCredentials.of().withClientId("my-client-id") .withClientSecret("my-secret") .withScopes("my-scopes") .build(), ServiceRegion.AWS_EU_CENTRAL_1.getOAuthTokenUrl(), ServiceRegion.AWS_EU_CENTRAL_1.getApiUrl());

    CategoryDraft categoryDraft = CategoryDraftBuilder.of()
            .name(LocalizedStringBuilder.of().addValue("en", "name").build())
            .slug(LocalizedStringBuilder.of().addValue("en", "slug").build())
            .description(LocalizedStringBuilder.of().addValue("en", "description").build())
            .externalId("random-id")
            .key("random-key")
            .metaDescription(LocalizedStringBuilder.of().addValue("en", "meta").build())
            .orderHint("hint")
            .build();

    Category category = apiRoot.withProjectKey("project-key")
            .categories()
            .post(categoryDraft)
            .executeBlocking()
            .getBody();`
jenschude commented 3 years ago

Without being able to take a closer look seems that your project resolved okhttp 3.x instead of v4.x. Could you check it and update your dependencies to use 4.x? Detailed look and fix may be possible next year ;-)

georgemarin commented 3 years ago

if it helps, please see attached my pom.xml

`<?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
<version>0.0.1-SNAPSHOT</version>

<properties>
    <java.version>11</java.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <spring-cloud-gcp.version>2.0.0-RC2</spring-cloud-gcp.version>
    <spring-cloud.version>2020.0.0-RC1</spring-cloud.version>
    <spring-boot.version>2.4.1</spring-boot.version>
    <testcontainers.version>1.15.1</testcontainers.version>
    <commercetools.version>2.0.0-beta.2</commercetools.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers</artifactId>
        <version>${testcontainers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>rabbitmq</artifactId>
        <version>${testcontainers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>${testcontainers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>gcloud</artifactId>
        <version>${testcontainers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.commercetools.sdk</groupId>
        <artifactId>commercetools-sdk-java-api</artifactId>
        <version>${commercetools.version}</version>
    </dependency>
    <dependency>
        <groupId>com.commercetools.sdk</groupId>
        <artifactId>commercetools-sdk-java-importapi</artifactId>
        <version>${commercetools.version}</version>
    </dependency>
    <dependency>
        <groupId>com.commercetools.sdk</groupId>
        <artifactId>commercetools-sdk-java-ml</artifactId>
        <version>${commercetools.version}</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>${spring-cloud-gcp.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>Application</mainClass>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
            <repository>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>bintray</name>
                <url>https://jcenter.bintray.com</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>bintray-plugins</name>
                <url>https://jcenter.bintray.com</url>
            </pluginRepository>
        </pluginRepositories>
        <id>bintray</id>
    </profile>
</profiles>

`

georgemarin commented 3 years ago

[INFO] +- com.commercetools.sdk:commercetools-sdk-java-api:jar:2.0.0-beta.2:compile [INFO] | +- com.commercetools.sdk:okhttp-client:jar:2.0.0-beta.2:compile [INFO] | | - com.squareup.okhttp3:okhttp:jar:3.14.9:runtime [INFO] | | - com.squareup.okio:okio:jar:1.17.2:runtime [INFO] | +- com.commercetools.sdk:rmf-java-base:jar:2.0.0-beta.2:compil

FYI, seems like okhttp3 dependency is commercetools depedency

jenschude commented 3 years ago

See https://github.com/commercetools/commercetools-sdk-java-v2/blob/f6db6caa808f97a759f2a9dd53781c284f4cc3d8/rmf/okhttp-client/build.gradle#L4

But as its a implementation dependency it can happen that direct dependency includes by other libs could be preferred.

And as you can see your project resolved to 3.14.9

jenschude commented 3 years ago

Hi

i just released a new beta version for the v2.0.0 (2.0.0-beta.3)

It's now mandatory to add a http client module as a dependency to avoid these kind of issues.

The default one would be:

    <dependency>
      <groupId>com.commercetools.sdk</groupId>
      <artifactId>commercetools-http-client</artifactId>
      <version>${commercetools.version}</version>
    </dependency>

This is an alias for the commercetools-okhttp-client4 module. If you are using okhttp v3 use commercetools-okhttp-client3 instead. The package okhttp-client has been deprecated due to said reasons

jenschude commented 3 years ago

The new version 2.0.0 has been released. Closing this issue now