Unirest JSONElement NoSuchMethodError - PullRequest
       16

Unirest JSONElement NoSuchMethodError

0 голосов
/ 03 февраля 2020

Я использую библиотеку Unirest для вызова общедоступной конечной точки REST со следующими строками кода:

public void callRest() {
    String url = "https://upstream.com/token"
    HttpResponse<JsonNode> response = (HttpResponse<JsonNode>) Unirest.post(url).
            field("username", "###").
            field("password", "###").
            field("grant_type", "password").
            field("client_id", "####").
            field("client_secret", "####").asJson().getBody();
}

Я получаю следующую ошибку:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.gson.Gson.newBuilder()Lcom/google/gson/GsonBuilder;
    at kong.unirest.json.JSONElement.<clinit>(JSONElement.java:39)
    at kong.unirest.JsonNode.<init>(JsonNode.java:44)
    at kong.unirest.JsonResponse.toJsonNode(JsonResponse.java:49)
    at kong.unirest.JsonResponse.getNode(JsonResponse.java:43)
    at kong.unirest.JsonResponse.<init>(JsonResponse.java:35)
    at kong.unirest.apache.BaseApacheClient.transformBody(BaseApacheClient.java:53)
    at kong.unirest.apache.ApacheClient.request(ApacheClient.java:127)
    at kong.unirest.BaseRequest.asJson(BaseRequest.java:213)

Строка, которая выделяется это

at kong.unirest.json.JSONElement.<clinit>(JSONElement.java:39)

, где самая неподходящая библиотека только что объявляет

private static transient final Gson PRETTY_GSON = new Gson().newBuilder().setPrettyPrinting().create();

в классе JSONElement. Я следовал точному руководству, данному в документации Unirest, которую вы можете найти здесь .

Я добавил следующие зависимости POM:

        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.3.00</version>
        </dependency>
        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.3.00</version>
            <classifier>standalone</classifier>
        </dependency>

1 Ответ

0 голосов
/ 03 февраля 2020

На самом деле это был конфликт версий с Unirest. Когда я удалил

        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.3.00</version>
            <classifier>standalone</classifier>
        </dependency> 

и получил результат всего на .asJson(), у меня все заработало.

...