Package org.kiwiproject.jaxrs.client
Class WebTargetHelper
java.lang.Object
org.kiwiproject.jaxrs.client.WebTargetHelper
- All Implemented Interfaces:
jakarta.ws.rs.client.WebTarget,jakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
Use with Jakarta REST
WebTarget instances to provide convenient functionality when adding query parameters.
Most of this functionality is intended for cases when you only want to add parameters when they are not null (or not
blank in the case of Strings). If you want a query parameter to be added regardless of whether a value is present
or not, use the regular queryParam method in WebTarget.
The methods provided by this helper class allow you to either require query parameters or include them only when
they have a value. When you require a query parameter, an IllegalArgumentException is thrown when
a caller does not supply a name or value. Other methods allow you to optionally include one or more query
parameters, as well as add them from a Map or a MultivaluedMap, such that only non-null/non-blank
values are added.
Usage example (assuming withClient is statically imported):
var response = withClient(client).target("/search")
.queryParamRequireNotBlank("q", query)
.queryParamIfNotBlank("sort", sort)
.queryParamIfNotBlank("page", page)
.queryParamIfNotBlank("limit", limit)
.queryParamFilterNotBlank("lang", languages)
.request()
.get();
This class implements WebTarget, and overridden methods return WebTargetHelper, so you can chain
methods as you normally would. For example, using withWebTarget:
var response = withWebTarget(originalTarget)
.path("/resolve/{id}")
.resolveTemplate("id", 42)
.queryParamIfNotBlank("format", format)
.queryParamIfNotNull("force", force)
.request()
.get();
-
Method Summary
Modifier and TypeMethodDescriptionjakarta.ws.rs.core.ConfigurationgetUri()jakarta.ws.rs.core.UriBuildermatrixParam(String name, Object... values) queryParam(String name, Object... values) queryParamFilterListNotBlank(String name, List<?> values) Adds any non-blank values to the given query parameter.queryParamFilterListNotNull(String name, List<?> values) Adds any non-null values to the given query parameter.queryParamFilterNotBlank(String name, String... values) Adds any non-blank values to the given query parameter.queryParamFilterNotBlank(String name, List<String> values) Deprecated, for removal: This API element is subject to removal in a future version.queryParamFilterNotBlank(String name, Stream<String> stream) Deprecated, for removal: This API element is subject to removal in a future version.queryParamFilterNotNull(String name, Object... values) Adds any non-null values to the given query parameter.queryParamFilterNotNull(String name, List<Object> values) Deprecated, for removal: This API element is subject to removal in a future version.queryParamFilterNotNull(String name, Stream<Object> stream) Deprecated, for removal: This API element is subject to removal in a future version.queryParamFilterObjectsNotBlank(String name, Object... values) Adds non-blank query parameters, converting each object to a string if necessary.queryParamFilterStreamNotBlank(String name, Stream<?> stream) Adds any non-blank values to the given query parameter.queryParamFilterStreamNotNull(String name, Stream<?> stream) Adds any non-null values to the given query parameter.queryParamIfNotBlank(String name, String value) Add the given query parameter only if bothnameandvalueare not blank.queryParamIfNotNull(String name, Object value) Add the given query parameter only ifnameis not blank andvalueis not null.queryParamRequireNotBlank(String name, String value) Add the required query parameter.queryParamRequireNotNull(String name, Object value) Add the required query parameter.<V> WebTargetHelperqueryParamsFromMap(Map<String, V> parameters) Adds non-null query parameters from the given map.<V> WebTargetHelperqueryParamsFromMultivaluedMap(jakarta.ws.rs.core.MultivaluedMap<String, V> parameters) Adds non-null query parameters from the given multivalued map.jakarta.ws.rs.client.Invocation.Builderrequest()jakarta.ws.rs.client.Invocation.Builderrequest(jakarta.ws.rs.core.MediaType... acceptedResponseTypes) jakarta.ws.rs.client.Invocation.BuilderresolveTemplate(String name, Object value) resolveTemplate(String name, Object value, boolean encodeSlashInPath) resolveTemplateFromEncoded(String name, Object value) resolveTemplates(Map<String, Object> templateValues) resolveTemplates(Map<String, Object> templateValues, boolean encodeSlashInPath) resolveTemplatesFromEncoded(Map<String, Object> templateValues) jakarta.ws.rs.client.WebTargetConvert the current state contained in this helper to a newWebTargetinstance.static WebTargetHelperwithWebTarget(jakarta.ws.rs.client.WebTarget webTarget) Create a new instance with the givenWebTarget.
-
Method Details
-
toWebTarget
public jakarta.ws.rs.client.WebTarget toWebTarget()Convert the current state contained in this helper to a newWebTargetinstance.- Returns:
- a new WebTarget instance
-
withWebTarget
Create a new instance with the givenWebTarget.- Parameters:
webTarget- the WebTarget to use- Returns:
- a new instance
-
queryParamRequireNotNull
Add the required query parameter.- Parameters:
name- the parameter namevalue- the parameter value- Returns:
- a new instance
- Throws:
IllegalArgumentException- ifnameis blank orvalueis null
-
queryParamIfNotNull
Add the given query parameter only ifnameis not blank andvalueis not null.- Parameters:
name- the parameter namevalue- the parameter value- Returns:
- a new instance if
nameandvalueare not blank, otherwise this instance
-
queryParamFilterNotNull
Adds any non-null values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance - See Also:
-
queryParamFilterNotNull
@Deprecated(since="4.10.0", forRemoval=true) @KiwiDeprecated(replacedBy="#queryParamFilterListNotNull(String name, List<?> values)", removeAt="5.0.0") public WebTargetHelper queryParamFilterNotNull(String name, List<Object> values) Deprecated, for removal: This API element is subject to removal in a future version.Adds any non-null values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance
-
queryParamFilterListNotNull
Adds any non-null values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance
-
queryParamFilterNotNull
@Deprecated(since="4.10.0", forRemoval=true) @KiwiDeprecated(replacedBy="#queryParamFilterStreamNotNull(String name, Stream<?> stream)", removeAt="5.0.0") public WebTargetHelper queryParamFilterNotNull(String name, Stream<Object> stream) Deprecated, for removal: This API element is subject to removal in a future version.Adds any non-null values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namestream- containing one or more parameter values- Returns:
- a new instance if
nameis not blank andstreamis not null, otherwise this instance
-
queryParamFilterStreamNotNull
Adds any non-null values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namestream- containing one or more parameter values- Returns:
- a new instance if
nameis not blank andstreamis not null, otherwise this instance
-
queryParamRequireNotBlank
Add the required query parameter.- Parameters:
name- the parameter namevalue- the parameter value- Returns:
- this instance
- Throws:
IllegalArgumentException- ifnameorvalueis blank
-
queryParamIfNotBlank
Add the given query parameter only if bothnameandvalueare not blank.- Parameters:
name- the parameter namevalue- the parameter value- Returns:
- a new instance if
nameis andvalueare not blank, otherwise this instance
-
queryParamFilterNotBlank
Adds any non-blank values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance - See Also:
-
queryParamFilterObjectsNotBlank
Adds non-blank query parameters, converting each object to a string if necessary.Any
nullor blank string values are ignored. Non-CharSequenceobjects are converted usingObject.toString().- Parameters:
name- the query parameter namevalues- the values to filter and add- Returns:
- a new instance with updated query parameters, or this instance if input is empty or invalid
- See Also:
-
queryParamFilterNotBlank
@Deprecated(since="4.10.0", forRemoval=true) @KiwiDeprecated(replacedBy="#queryParamFilterListNotBlank(String name, List<String> values)", removeAt="5.0.0") public WebTargetHelper queryParamFilterNotBlank(String name, List<String> values) Deprecated, for removal: This API element is subject to removal in a future version.Adds any non-blank values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance
-
queryParamFilterListNotBlank
Adds any non-blank values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namevalues- one or more parameter values- Returns:
- a new instance if
nameis not blank andvaluesis not null or empty, otherwise this instance
-
queryParamFilterNotBlank
@Deprecated(since="4.10.0", forRemoval=true) @KiwiDeprecated(replacedBy="queryParamFilterStreamNotBlank(String name, Stream<String> stream)", removeAt="5.0.0") public WebTargetHelper queryParamFilterNotBlank(String name, Stream<String> stream) Deprecated, for removal: This API element is subject to removal in a future version.Adds any non-blank values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namestream- containing one or more parameter values- Returns:
- a new instance if
nameis not blank andstreamis not null, otherwise this instance
-
queryParamFilterStreamNotBlank
Adds any non-blank values to the given query parameter. Ifnameis blank, this is a no-op.- Parameters:
name- the parameter namestream- containing one or more parameter values- Returns:
- a new instance if
nameis not blank andstreamis not null, otherwise this instance
-
queryParamsFromMap
Adds non-null query parameters from the given map. All map keys must be non-null.- Type Parameters:
V- the type of keys in the map- Parameters:
parameters- a map representing the query parameters- Returns:
- a new instance if
parametersis not null or empty, otherwise this instance - Implementation Note:
- This method is distinct from
queryParamsFromMultivaluedMap(MultivaluedMap)because theMultivaluedMapinterface extends the regular JavaMapand under certain circumstances this method will be called even when the argument is actually aMultivaluedMap. By having separate and distinctly named methods, it unambiguously avoids this potential problem, at the expense of callers needing to make a concrete decision on which method to call. However, in most situations that we have seen (in our own code) this is not an issue. For example,UriInfo.getQueryParameters()returns a MultivaluedMap, which makes it easy to select the appropriate method to call.
-
queryParamsFromMultivaluedMap
public <V> WebTargetHelper queryParamsFromMultivaluedMap(jakarta.ws.rs.core.MultivaluedMap<String, V> parameters) Adds non-null query parameters from the given multivalued map. All map keys must be non-null.- Type Parameters:
V- the type of keys in the map- Parameters:
parameters- a multivalued map representing the query parameters- Returns:
- a new instance if
parametersis not null or empty, otherwise this instance - Implementation Note:
- See implementation note on
queryParamsFromMap(Map)for an explanation why this method is named separately and distinctly.
-
getUri
- Specified by:
getUriin interfacejakarta.ws.rs.client.WebTarget
-
getUriBuilder
public jakarta.ws.rs.core.UriBuilder getUriBuilder()- Specified by:
getUriBuilderin interfacejakarta.ws.rs.client.WebTarget
-
path
- Specified by:
pathin interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplate
- Specified by:
resolveTemplatein interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplate
- Specified by:
resolveTemplatein interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplateFromEncoded
- Specified by:
resolveTemplateFromEncodedin interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplates
- Specified by:
resolveTemplatesin interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplates
public WebTargetHelper resolveTemplates(Map<String, Object> templateValues, boolean encodeSlashInPath) - Specified by:
resolveTemplatesin interfacejakarta.ws.rs.client.WebTarget
-
resolveTemplatesFromEncoded
- Specified by:
resolveTemplatesFromEncodedin interfacejakarta.ws.rs.client.WebTarget
-
matrixParam
- Specified by:
matrixParamin interfacejakarta.ws.rs.client.WebTarget
-
queryParam
- Specified by:
queryParamin interfacejakarta.ws.rs.client.WebTarget
-
request
public jakarta.ws.rs.client.Invocation.Builder request()- Specified by:
requestin interfacejakarta.ws.rs.client.WebTarget
-
request
- Specified by:
requestin interfacejakarta.ws.rs.client.WebTarget
-
request
public jakarta.ws.rs.client.Invocation.Builder request(jakarta.ws.rs.core.MediaType... acceptedResponseTypes) - Specified by:
requestin interfacejakarta.ws.rs.client.WebTarget
-
getConfiguration
public jakarta.ws.rs.core.Configuration getConfiguration()- Specified by:
getConfigurationin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
property
- Specified by:
propertyin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
register
- Specified by:
registerin interfacejakarta.ws.rs.core.Configurable<jakarta.ws.rs.client.WebTarget>
-
queryParamFilterListNotBlank(String, List)