我正在使用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文档中给出的指导原则,你可以找到here。
我添加的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>发布于 2020-02-03 23:31:39
实际上,这是与Unirest的版本冲突。当我删除
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.3.00</version>
<classifier>standalone</classifier>
</dependency> 并通过.asJson()获取结果,然后一切都为我工作。
https://stackoverflow.com/questions/60042060
复制相似问题