首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unirest JSONElement NoSuchMethodError

Unirest JSONElement NoSuchMethodError
EN

Stack Overflow用户
提问于 2020-02-03 23:15:22
回答 1查看 219关注 0票数 0

我正在使用Unirest库调用公共可用的REST端点,代码如下:

代码语言:javascript
复制
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();
}

我收到以下错误:

代码语言:javascript
复制
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)

最突出的一行是

代码语言:javascript
复制
at kong.unirest.json.JSONElement.<clinit>(JSONElement.java:39)

其中最统一的库只是声明

代码语言:javascript
复制
private static transient final Gson PRETTY_GSON = new Gson().newBuilder().setPrettyPrinting().create();

在JSONElement类中。我遵循了Unirest文档中给出的指导原则,你可以找到here

我添加的POM依赖项如下:

代码语言:javascript
复制
        <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>
EN

回答 1

Stack Overflow用户

发布于 2020-02-03 23:31:39

实际上,这是与Unirest的版本冲突。当我删除

代码语言:javascript
复制
        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.3.00</version>
            <classifier>standalone</classifier>
        </dependency> 

并通过.asJson()获取结果,然后一切都为我工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60042060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档