Class Parameter
":id"). Parameters can be required or optional. Optional parameters use a trailing question
mark (e.g. ":id?") and must be at the end of the route. A route like "/users/:id?" matches both
"/users" and "/users/123".
Multiple trailing optional parameters are allowed: "/a/:b?/:c?" matches "/a", "/a/1", and
"/a/1/2". Optional parameters after required ones are also valid: "/a/:b/:c?" matches "/a/1" and
"/a/1/2".
Parameter values are automatically decoded when retrieved via get(String) or getOrDefault(String, String).
Use getRaw(String) to access the raw/encoded value as it appears in the URL.
When an optional parameter is not present in the path, get(String) returns null and has(String)
returns false. Use getOrDefault(String, String) to provide a fallback value.
To build URLs with encoded parameter values, use encodePath(String, String...) or encode(String).
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDecodes a URL-encoded parameter value back to its original form.static StringEncodes a single parameter value for safe inclusion in a URL path segment.static StringencodePath(String route, String... values) Builds a URL path from a route template and raw parameter values.Returns the decoded parameter value.getOrDefault(String name, String defaultValue) Returns the decoded parameter value, or the given default if the parameter doesn't exist.Returns the raw/encoded parameter value as it appears in the URL.booleanReturns whether a parameter with the given name exists.booleanisEmpty()path()Returns the path this parameter was parsed from.
-
Field Details
-
EMPTY
-
-
Method Details
-
encode
Encodes a single parameter value for safe inclusion in a URL path segment. Special characters like/,?,#,&,=, and spaces are percent-encoded.Pass raw/decoded values to this method. Do not pass already-encoded values, as this will result in double encoding.
- Parameters:
value- the raw parameter value to encode- Returns:
- the percent-encoded value
- Throws:
IllegalArgumentException- if value is null
-
decode
Decodes a URL-encoded parameter value back to its original form.- Parameters:
value- the percent-encoded value to decode- Returns:
- the decoded value
- Throws:
IllegalArgumentException- if value is null
-
encodePath
Builds a URL path from a route template and raw parameter values. Values are encoded automatically usingencode(String)and substituted into the template in order of parameter appearance.Optional parameters (e.g.
":name?") can be omitted. When fewer values are provided than the total number of parameters, trailing optional parameter segments are omitted from the output path.Example:
Parameter.encodePath("/resource/:name", "my/file") // returns "/resource/my%2Ffile" Parameter.encodePath("/time/:area/:location", "America", "New_York") // returns "/time/America/New_York" Parameter.encodePath("/users/:id?") // returns "/users" Parameter.encodePath("/users/:id?", "123") // returns "/users/123"- Parameters:
route- the route template with:paramand:param?placeholdersvalues- the raw parameter values in the order they appear in the route. Required parameters must all be provided; optional parameters can be omitted from the end.- Returns:
- the complete URL path with encoded parameter values
- Throws:
IllegalArgumentException- if the number of values is less than the number of required parameters or greater than the total number of parameters, or if any required value is null
-
isEmpty
public boolean isEmpty() -
has
Returns whether a parameter with the given name exists.- Parameters:
name- the parameter name- Returns:
- true if the parameter exists
-
get
-
getOrDefault
Returns the decoded parameter value, or the given default if the parameter doesn't exist.- Parameters:
name- the parameter namedefaultValue- the default value to return if the parameter doesn't exist- Returns:
- the decoded parameter value, or the default value
-
getRaw
-
path
-