在使用 Javax webtarget 客户端发送 HTTPS 请求时,如何避免对包含“:[”的查询参数进行编码?
In sending HTTPS request using Javax webtarget client, how to avoid encoding of query params that contain ":["?
我正在使用 WebTarget 发送带有查询参数的 GET 请求。
这是我需要生成的预期请求:
{{host}}/svc/lookupservice/v2/findItems?filter=creationdate:[2020-05-01T00:08:17.000Z..]&limit=25&offset=0&userType=seller&userid=11234567
但是使用webtarget如下:
import javax.ws.rs.client.Client;
...
...
class A {
private Client client;
public void apiCall(String userId, String startDate, String endDate) {
String endpoint = (String)this.client.getConfiguration().getProperty("jaxrs.client.endpointuri");
WebTarget webTarget = this.client.target(endpoint).path("/lookupservice/v2/findItems")
.queryParam("userType", new Object[]{"seller"})
.queryParam("sort", new Object[]{"creationdate"})
.queryParam("limit", new Object[]{150})
.queryParam("offset", new Object[]{0})
.queryParam("filter", new Object[]{"creationdate:[" + startDate + ".." + endDate + "]"})
.queryParam("userid", new Object[]{userId});
...
}
...
}
class B {
main(..) {
Date endDate = new Date();
Date startDate = new Date(endDate.getTime() - Duration.ofDays(30).toMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
sDate = formatter.format(startDate);
eDate = formatter.format(endDate);
new A().apiCall(userId, sDate, eDate);
}
}
以上生成的 webTarget url 为
JerseyWebTarget { https://svc1.abc.qa.def.com/svc/lookupservice/v2/findItems?userType=seller&sort=creationdate&limit=150&offset=0&filter=creationdate%3A%5B2022-03-08T15%3A26%3A35.951Z..2022-04-07T16%3A26%3A35.951Z%5D&userid=1005732584 }
它在创建日期编码 :[
。
如何避免?
通过不使用 webTarget 的 .queryParams()
而是通过查询传递 URI 来解决。
String query = "?userType=seller&limit=25&offset=0&userid=" + userId + "&filter=creationdate:[" + startDate + ".." + endDate + "]";
URI uri = URI.create(endpoint + "/lookupservice/v2/findItems"+ query);
WebTarget webTarget = client.target(uri);
我正在使用 WebTarget 发送带有查询参数的 GET 请求。
这是我需要生成的预期请求:
{{host}}/svc/lookupservice/v2/findItems?filter=creationdate:[2020-05-01T00:08:17.000Z..]&limit=25&offset=0&userType=seller&userid=11234567
但是使用webtarget如下:
import javax.ws.rs.client.Client;
...
...
class A {
private Client client;
public void apiCall(String userId, String startDate, String endDate) {
String endpoint = (String)this.client.getConfiguration().getProperty("jaxrs.client.endpointuri");
WebTarget webTarget = this.client.target(endpoint).path("/lookupservice/v2/findItems")
.queryParam("userType", new Object[]{"seller"})
.queryParam("sort", new Object[]{"creationdate"})
.queryParam("limit", new Object[]{150})
.queryParam("offset", new Object[]{0})
.queryParam("filter", new Object[]{"creationdate:[" + startDate + ".." + endDate + "]"})
.queryParam("userid", new Object[]{userId});
...
}
...
}
class B {
main(..) {
Date endDate = new Date();
Date startDate = new Date(endDate.getTime() - Duration.ofDays(30).toMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
sDate = formatter.format(startDate);
eDate = formatter.format(endDate);
new A().apiCall(userId, sDate, eDate);
}
}
以上生成的 webTarget url 为
JerseyWebTarget { https://svc1.abc.qa.def.com/svc/lookupservice/v2/findItems?userType=seller&sort=creationdate&limit=150&offset=0&filter=creationdate%3A%5B2022-03-08T15%3A26%3A35.951Z..2022-04-07T16%3A26%3A35.951Z%5D&userid=1005732584 }
它在创建日期编码 :[
。
如何避免?
通过不使用 webTarget 的 .queryParams()
而是通过查询传递 URI 来解决。
String query = "?userType=seller&limit=25&offset=0&userid=" + userId + "&filter=creationdate:[" + startDate + ".." + endDate + "]";
URI uri = URI.create(endpoint + "/lookupservice/v2/findItems"+ query);
WebTarget webTarget = client.target(uri);