泽西休息客户端不添加查询参数 [英] Jersey rest client not adding query parameters
问题描述
我正在尝试为Google搜索API创建一个简单的Jersey rest客户端.
I'm trying to make a simple jersey rest client for google search api.
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://www.googleapis.com/customsearch/v1");
target.queryParam("q", "mobile");
Response response = target.request().get();
System.out.println(response.readEntity(String.class));
您已经注意到,我没有包含key
和cx
.不用担心,这只是一个简单的演示.
访问网址https://www.googleapis.com/customsearch/v1?q=mobile
时,响应为
As you've noticed I haven't included key
and cx
. Don't worry about that, it's just a simple demo.
When visiting the url https://www.googleapis.com/customsearch/v1?q=mobile
, the response is
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
这是正确的,因为我没有包含key
和cx
.当我执行上面的代码时,我得到的响应是
Which is correct since I haven't included key
and cx
. When I execute the code above, the response I'm getting is
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: q",
"locationType": "parameter",
"location": "q"
}
],
"code": 400,
"message": "Required parameter: q"
}
}
这等效于不带任何参数(https://www.googleapis.com/customsearch/v1
)访问url,尽管我已经添加了此target.queryParam("q", "mobile");
.我在做错什么吗?
Which is equivalent of visiting the url without any parameters (https://www.googleapis.com/customsearch/v1
), although I've added this target.queryParam("q", "mobile");
. Am I doing something wrong?
上面的代码属于Mavenized项目,其依赖项为
The code above belongs to a mavenized project and the dependency is
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.14</version>
</dependency>
推荐答案
链接通话
Response response= client.target("https://www.googleapis.com/customsearch/v1")
.queryParam("q", "mobile").request().get();
来自文档:
返回:一个新的目标实例.
注意:-如果未链接,则获取新创建的 webtarget 实例并使用它.
Note :- If not chaining then get newly created webtarget instance and use it.
WebTarget webTarget = client.target(snapshotGeneratorUrl);
webTarget = webTarget.queryParam("foo","foo").queryParam("bar",bar);
Response response = webTarget.request().get();
这篇关于泽西休息客户端不添加查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!