准备 Invocation Builder 以发送查询参数 - jax rs
Preparing Invocation Builder to send query parameters - jax rs
我想使用如下所示的 url 进行 GET:
private static final String TEMPLATE_URI = /tfe/{param1}/{param2};
为此我制作了一张地图:
final Map<String, Object> templateValues = new HashMap<>();
templateValues.put(param1, someValue1);
templateValues.put(param2, someValue2);
然后我使用 invocationBuilder 来匹配值并像这样发送它:
final Invocation.Builder invocationBuilder = getInvocationBuilder(TEMPLATE_URI, templateValues);
final Response response = invocationBuilder.get();
getInvocationBuilder 如下所示:
protected Invocation.Builder getInvocationBuilder(final String templateUri, final Map<String, Object> templateValues) {
final String baseUrl = ApplicationProperties.getInstance().getProperty("baseUrl");
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target(baseUrl);
final WebTarget template = target.path(templateUri);
final WebTarget resource = template.resolveTemplates(templateValues);
final Invocation.Builder invocationBuilder = resource.request(MediaType.APPLICATION_JSON_TYPE);
return invocationBuilder;
}
问题:一切正常,但现在我还想发送查询参数,这不是强制性的。我想从 url 的接收器上读取它们,如下所示:
/tfe/{param1}/{param2}?{param3}=3&{param4}=4
我如何修改 getInvocationBuilder 来处理这些?我想出了如何在接收端获取它们,但在这里我需要帮助以某种方式将它们填充到 invocationbuilder 中。谢谢!
您可以为查询参数创建另一个映射并执行以下操作:
for (Entry<String, Object> entry : queryParams.entrySet()) {
target.queryParam(entry.getKey(), entry.getValue());
}
是的peeskillet!
你说得对,我修改了 getInvocationBuilder 方法,现在它应该可以工作了:
protected Invocation.Builder getInvocationBuilder(final String templateUri, final Map<String, Object> templateValues,
final Map<String, Object> queryParameterValues) {
final String baseUrl = ApplicationProperties.getInstance().getProperty("ucp.baseUrl");
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target(baseUrl);
final WebTarget template = target.path(templateUri);
final WebTarget resource = template.resolveTemplates(templateValues);
if (queryParameterValues != null) {
final Iterator iterator = queryParameterValues.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry pair = (Map.Entry) iterator.next();
resource.queryParam((String) pair.getKey(), pair.getValue());
}
}
final Invocation.Builder invocationBuilder = resource.request(MediaType.APPLICATION_JSON_TYPE);
return invocationBuilder;
}
查询参数作为另一个映射提供给方法,现在调用方法如下:
final Map<String, Object> queryParameterValues = new HashMap<>();
queryParameterValues.put(REQUIRED_OTD_FIELDS_QUERY_PARAM, REQUIRED_OTD_FIELDS_QUERY_PARAM_VALUE);
谢谢!
我想使用如下所示的 url 进行 GET:
private static final String TEMPLATE_URI = /tfe/{param1}/{param2};
为此我制作了一张地图:
final Map<String, Object> templateValues = new HashMap<>();
templateValues.put(param1, someValue1);
templateValues.put(param2, someValue2);
然后我使用 invocationBuilder 来匹配值并像这样发送它:
final Invocation.Builder invocationBuilder = getInvocationBuilder(TEMPLATE_URI, templateValues);
final Response response = invocationBuilder.get();
getInvocationBuilder 如下所示:
protected Invocation.Builder getInvocationBuilder(final String templateUri, final Map<String, Object> templateValues) {
final String baseUrl = ApplicationProperties.getInstance().getProperty("baseUrl");
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target(baseUrl);
final WebTarget template = target.path(templateUri);
final WebTarget resource = template.resolveTemplates(templateValues);
final Invocation.Builder invocationBuilder = resource.request(MediaType.APPLICATION_JSON_TYPE);
return invocationBuilder;
}
问题:一切正常,但现在我还想发送查询参数,这不是强制性的。我想从 url 的接收器上读取它们,如下所示:
/tfe/{param1}/{param2}?{param3}=3&{param4}=4
我如何修改 getInvocationBuilder 来处理这些?我想出了如何在接收端获取它们,但在这里我需要帮助以某种方式将它们填充到 invocationbuilder 中。谢谢!
您可以为查询参数创建另一个映射并执行以下操作:
for (Entry<String, Object> entry : queryParams.entrySet()) {
target.queryParam(entry.getKey(), entry.getValue());
}
是的peeskillet!
你说得对,我修改了 getInvocationBuilder 方法,现在它应该可以工作了:
protected Invocation.Builder getInvocationBuilder(final String templateUri, final Map<String, Object> templateValues,
final Map<String, Object> queryParameterValues) {
final String baseUrl = ApplicationProperties.getInstance().getProperty("ucp.baseUrl");
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target(baseUrl);
final WebTarget template = target.path(templateUri);
final WebTarget resource = template.resolveTemplates(templateValues);
if (queryParameterValues != null) {
final Iterator iterator = queryParameterValues.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry pair = (Map.Entry) iterator.next();
resource.queryParam((String) pair.getKey(), pair.getValue());
}
}
final Invocation.Builder invocationBuilder = resource.request(MediaType.APPLICATION_JSON_TYPE);
return invocationBuilder;
}
查询参数作为另一个映射提供给方法,现在调用方法如下:
final Map<String, Object> queryParameterValues = new HashMap<>();
queryParameterValues.put(REQUIRED_OTD_FIELDS_QUERY_PARAM, REQUIRED_OTD_FIELDS_QUERY_PARAM_VALUE);
谢谢!