Skip to content

Commit 3e502d4

Browse files
committed
Document removal of duplicate slashes in UriComponentsBuilder
Closes gh-26457
1 parent 667256a commit 3e502d4

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

spring-web/src/main/java/org/springframework/web/util/UriBuilder.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,39 +79,37 @@ public interface UriBuilder {
7979

8080
/**
8181
* Append to the path of this builder.
82-
* <p>The given value is appended as-is without any checks for slashes other
83-
* than to clean up duplicates. For example:
82+
* <p>The given value is appended as-is to previous {@link #path(String) path}
83+
* values without inserting any additional slashes. For example:
8484
* <pre class="code">
8585
*
8686
* builder.path("/first-").path("value/").path("/{id}").build("123")
8787
*
8888
* // Results is "/first-value/123"
8989
* </pre>
90-
* <p>By contrast {@link #pathSegment(String...)} builds the path from
91-
* individual path segments and in that case slashes are inserted transparently.
92-
* In some cases you may use a combination of both {@code pathSegment} and
93-
* {@code path}. For example:
90+
* <p>By contrast {@link #pathSegment(String...) pathSegment} does insert
91+
* slashes between individual path segments. For example:
9492
* <pre class="code">
9593
*
9694
* builder.pathSegment("first-value", "second-value").path("/")
9795
*
9896
* // Results is "/first-value/second-value/"
99-
*
10097
* </pre>
101-
* <p>If a URI variable value contains slashes, whether those are encoded or
102-
* not depends on the configured encoding mode. See
103-
* {@link UriComponentsBuilder#encode()}, or if using
104-
* {@code UriComponentsBuilder} via {@link DefaultUriBuilderFactory}
105-
* (e.g. {@code WebClient} or {@code RestTemplate}) see its
106-
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode} property.
98+
* <p>The resulting full path is normalized to eliminate duplicate slashes.
99+
* <p><strong>Note:</strong> When inserting a URI variable value that
100+
* contains slashes in a {@link #path(String) path}, whether those are
101+
* encoded depends on the configured encoding mode. For more details, see
102+
* {@link UriComponentsBuilder#encode()}, or otherwise if building URIs
103+
* indirectly via {@code WebClient} or {@code RestTemplate}, see its
104+
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode}.
107105
* Also see the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding">
108106
* URI Encoding</a> section of the reference docs.
109107
* @param path the URI path
110108
*/
111109
UriBuilder path(String path);
112110

113111
/**
114-
* Override the existing path.
112+
* Override the current path.
115113
* @param path the URI path, or {@code null} for an empty path
116114
*/
117115
UriBuilder replacePath(@Nullable String path);
@@ -123,24 +121,23 @@ public interface UriBuilder {
123121
* builder.pathSegment("first-value", "second-value", "{id}").build("123")
124122
*
125123
* // Results is "/first-value/second-value/123"
126-
*
127124
* </pre>
128125
* <p>If slashes are present in a path segment, they are encoded:
129126
* <pre class="code">
130127
*
131128
* builder.pathSegment("ba/z", "{id}").build("a/b")
132129
*
133130
* // Results is "/ba%2Fz/a%2Fb"
134-
*
135131
* </pre>
136132
* To insert a trailing slash, use the {@link #path} builder method:
137133
* <pre class="code">
138134
*
139135
* builder.pathSegment("first-value", "second-value").path("/")
140136
*
141137
* // Results is "/first-value/second-value/"
142-
*
143138
* </pre>
139+
* <p>Empty path segments are ignored and therefore duplicate slashes do not
140+
* appear in the resulting full path.
144141
* @param pathSegments the URI path segments
145142
*/
146143
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;

0 commit comments

Comments
 (0)