Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
Created July 28, 2022 08:44
Show Gist options
  • Select an option

  • Save dehidehidehi/26145b5d43820eaf758d87f8c318560a to your computer and use it in GitHub Desktop.

Select an option

Save dehidehidehi/26145b5d43820eaf758d87f8c318560a to your computer and use it in GitHub Desktop.
Builds a basic url: path params and query params from Maps.
static URI buildUri(String baseUrl, LinkedHashMap<String, String> pathParams, LinkedHashMap<String, String> queryParams) {
StringBuilder sb = new StringBuilder(baseUrl);
if (Objects.nonNull(pathParams))
sb.append("/").append(String.join("/", pathParams.values()));
if (Objects.nonNull(queryParams)) {
List<String> keyValueStrings = queryParams.entrySet().stream().map(
e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toUnmodifiableList());
sb.append("?").append(String.join("&", keyValueStrings));
}
return URI.create(sb.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment