Klasse UriExpander
java.lang.Object
de.kamillionlabs.hateoflux.linkbuilder.UriExpander
Utility class for expanding URI templates. Provides methods to expand URI templates using either ordered parameters
or named parameters from a map. The class handles templates indicated by placeholders enclosed in curly braces {}.
-
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic StringExpands the URI template using a list of anonymous parameters provided in the order they appear within the template.static StringExpands the URI template using a map of named path or query parameters.static StringExpands the URI template using a map of named path or query parameters.static StringremovePagingParameters(String inputUrl) Removes existing paging query parameterspage,size, andsort.
-
Methodendetails
-
expand
Expands the URI template using a list of anonymous parameters provided in the order they appear within the template. Placeholders for parameters follow the structure suggested by RFC6570. Given thatvaris a placeholder, i.e., a templated variable, the following applies:{var}is a mandatory variable.{?var}is an optional variable used specifically as a query parameter.{?var1,var2}are two optional query parameters.
expand()instead).
Example usage:
String template = "/users/{userId}/posts{?limit,page}" String expanded = expand(template, 42, 10, 2); // Outputs: /users/42/posts?limit=10&page=2- Parameter:
uriAsTemplate- URI template containing placeholdersparameters- a sequence of objects that correspond in order to the placeholders in the URI template- Gibt zurück:
- the expanded or original URI if expansion is not applicable
- Löst aus:
IllegalArgumentException- if template and parameters are incompatible
-
expand
public static String expand(String uriAsTemplate, Map<String, ?> parameters, boolean collectionRenderedAsComposite) Expands the URI template using a map of named path or query parameters. The full documentation can be found atexpand(String, Map). This variation ofexpand()adds the ability to influence how exploded parameters are rendered when a collection is provided.Example usages:
Exploded Query Parameter with Non-Composite Rendering (
false)var map = Map.of("keyWords", List.of("blue","active")); String expanded = expand("/users{?keyWords*}", map, false); ^^^^^ // Outputs: /users?keyWords=blue,activeExploded Query Parameter with Composite Rendering (
true)var map = Map.of("keyWords", List.of("blue","active")); String expanded = expand("/users{?keyWords*}", map, true); ^^^^ // Outputs: /users?keyWords=blue&keyWords=active- Parameter:
uriAsTemplate- URI template containing placeholdersparameters- a map containing key-value pairs where keys match the placeholders' names. Values for exploded parameters can be aCollection.collectionRenderedAsComposite- specifies whether the collection should be rendered as composite (true) or non-composite (false)- Gibt zurück:
- the expanded or original URI if expansion is not applicable
- Löst aus:
IllegalArgumentException- if template and parameters are incompatible
-
expand
Expands the URI template using a map of named path or query parameters. If the template contains no placeholders, the original string is returned. Placeholders for parameters follow the structure suggested by RFC6570. Given thatvaris a placeholder, i.e., a templated variable, the following applies:{var}is a mandatory variable.{?var}is an optional variable used specifically as a query parameter.{?var1,var2}are two optional query parameters.{?var*}is an exploded query parameter, i.e., it can represent a list.
- A collection of values is only allowed for query parameters.
- To accept a collection as a value, a query parameter must be marked with the explode modifier ('*' i.e., asterisk).
- The expansion of exploded parameters is configurable via
expand(String, Map, boolean). It can be expanded in a composite or non-composite way. - This method expands parameters in a non-composite way by default (e.g., ?var=1,2 as opposed to ?var=1&var=2).
Example usages:
Path and Query Parametersvar map = Map.of("id", 15, "limit", 50, "page", 2); String expanded = expand("/users/{id}/activity{?limit,page}", map); // Outputs: /users/15/activity?limit=50&page=2Unused Query Parameters in Template
var map = Map.of("id", 15); String expanded = expand("/users/{id}/activity{?limit,page}", map); // Outputs: /users/15/activityExploded Query Parameter
var map = Map.of("keyWords", List.of("blue","active") "page", 3)); String expanded = expand("/users{?keyWords*,page}", map); // Outputs: /users?keyWords=blue,active&page=3- Parameter:
uriAsTemplate- URI template containing placeholdersparameters- a map containing key-value pairs, where keys match the placeholders' names. Values for exploded parameters are allowed to be aCollection- Gibt zurück:
- the expanded or original URI if expansion is not applicable
- Löst aus:
IllegalArgumentException- if template and parameters are incompatible
-
removePagingParameters
Removes existing paging query parameterspage,size, andsort. Any other query parameter is ignored. If as a result the URI has no query paramters anymore, the '?' is removed.- Parameter:
inputUrl- input URL to remove paging parameters from- Gibt zurück:
- sanitized URL
-