Document removal of duplicate slashes in UriComponentsBuilder

Closes gh-26457
This commit is contained in:
Rossen Stoyanchev 2021-02-11 20:57:03 +00:00
parent 667256adf9
commit 3e502d4739
1 changed files with 15 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,31 +79,29 @@ public interface UriBuilder {
/** /**
* Append to the path of this builder. * Append to the path of this builder.
* <p>The given value is appended as-is without any checks for slashes other * <p>The given value is appended as-is to previous {@link #path(String) path}
* than to clean up duplicates. For example: * values without inserting any additional slashes. For example:
* <pre class="code"> * <pre class="code">
* *
* builder.path("/first-").path("value/").path("/{id}").build("123") * builder.path("/first-").path("value/").path("/{id}").build("123")
* *
* // Results is "/first-value/123" * // Results is "/first-value/123"
* </pre> * </pre>
* <p>By contrast {@link #pathSegment(String...)} builds the path from * <p>By contrast {@link #pathSegment(String...) pathSegment} does insert
* individual path segments and in that case slashes are inserted transparently. * slashes between individual path segments. For example:
* In some cases you may use a combination of both {@code pathSegment} and
* {@code path}. For example:
* <pre class="code"> * <pre class="code">
* *
* builder.pathSegment("first-value", "second-value").path("/") * builder.pathSegment("first-value", "second-value").path("/")
* *
* // Results is "/first-value/second-value/" * // Results is "/first-value/second-value/"
*
* </pre> * </pre>
* <p>If a URI variable value contains slashes, whether those are encoded or * <p>The resulting full path is normalized to eliminate duplicate slashes.
* not depends on the configured encoding mode. See * <p><strong>Note:</strong> When inserting a URI variable value that
* {@link UriComponentsBuilder#encode()}, or if using * contains slashes in a {@link #path(String) path}, whether those are
* {@code UriComponentsBuilder} via {@link DefaultUriBuilderFactory} * encoded depends on the configured encoding mode. For more details, see
* (e.g. {@code WebClient} or {@code RestTemplate}) see its * {@link UriComponentsBuilder#encode()}, or otherwise if building URIs
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode} property. * indirectly via {@code WebClient} or {@code RestTemplate}, see its
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode}.
* Also see the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding"> * Also see the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding">
* URI Encoding</a> section of the reference docs. * URI Encoding</a> section of the reference docs.
* @param path the URI path * @param path the URI path
@ -111,7 +109,7 @@ public interface UriBuilder {
UriBuilder path(String path); UriBuilder path(String path);
/** /**
* Override the existing path. * Override the current path.
* @param path the URI path, or {@code null} for an empty path * @param path the URI path, or {@code null} for an empty path
*/ */
UriBuilder replacePath(@Nullable String path); UriBuilder replacePath(@Nullable String path);
@ -123,7 +121,6 @@ public interface UriBuilder {
* builder.pathSegment("first-value", "second-value", "{id}").build("123") * builder.pathSegment("first-value", "second-value", "{id}").build("123")
* *
* // Results is "/first-value/second-value/123" * // Results is "/first-value/second-value/123"
*
* </pre> * </pre>
* <p>If slashes are present in a path segment, they are encoded: * <p>If slashes are present in a path segment, they are encoded:
* <pre class="code"> * <pre class="code">
@ -131,7 +128,6 @@ public interface UriBuilder {
* builder.pathSegment("ba/z", "{id}").build("a/b") * builder.pathSegment("ba/z", "{id}").build("a/b")
* *
* // Results is "/ba%2Fz/a%2Fb" * // Results is "/ba%2Fz/a%2Fb"
*
* </pre> * </pre>
* To insert a trailing slash, use the {@link #path} builder method: * To insert a trailing slash, use the {@link #path} builder method:
* <pre class="code"> * <pre class="code">
@ -139,8 +135,9 @@ public interface UriBuilder {
* builder.pathSegment("first-value", "second-value").path("/") * builder.pathSegment("first-value", "second-value").path("/")
* *
* // Results is "/first-value/second-value/" * // Results is "/first-value/second-value/"
*
* </pre> * </pre>
* <p>Empty path segments are ignored and therefore duplicate slashes do not
* appear in the resulting full path.
* @param pathSegments the URI path segments * @param pathSegments the URI path segments
*/ */
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException; UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;