Skip to content

Commit 81ba3b3

Browse files
committed
Dropped RequestEntity's template variable methods in order to remove dependencies on org.springframework.web
Instead, as outlined in the revised javadoc, let's recommend manual UriTemplate usage for RequestEntity URI input. Issue: SPR-11752
1 parent 7b93cef commit 81ba3b3

File tree

2 files changed

+19
-192
lines changed

2 files changed

+19
-192
lines changed

spring-web/src/main/java/org/springframework/http/RequestEntity.java

Lines changed: 8 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import java.net.URI;
2020
import java.nio.charset.Charset;
2121
import java.util.Arrays;
22-
import java.util.Map;
2322

2423
import org.springframework.util.Assert;
2524
import org.springframework.util.MultiValueMap;
2625
import org.springframework.util.ObjectUtils;
27-
import org.springframework.web.util.UriTemplate;
2826

2927
/**
3028
* Extension of {@link HttpEntity} that adds a {@linkplain HttpMethod method} and
@@ -35,10 +33,17 @@
3533
* {@link org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange()}:
3634
* <pre class="code">
3735
* MyRequest body = ...
38-
* RequestEntity&lt;MyRequest&gt; request = RequestEntity.post(&quot;http://example.com/{foo}&quot;, &quot;bar&quot;).accept(MediaType.APPLICATION_JSON).body(body);
36+
* RequestEntity&lt;MyRequest&gt; request = RequestEntity.post(new URI(&quot;http://example.com/bar&quot;).accept(MediaType.APPLICATION_JSON).body(body);
3937
* ResponseEntity&lt;MyResponse&gt; response = template.exchange(request, MyResponse.class);
4038
* </pre>
4139
*
40+
* <p>If you would like to provide a URI template with variables, consider using
41+
* {@link org.springframework.web.util.UriTemplate}:
42+
* <pre class="code">
43+
* URI uri = new UriTemplate(&quot;http://example.com/{foo}&quot;").expand(&quot;bar&quot;);
44+
* RequestEntity&lt;MyRequest&gt; request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
45+
* </pre>
46+
*
4247
* <p>Can also be used in Spring MVC, as a parameter in a @Controller method:
4348
* <pre class="code">
4449
* &#64;RequestMapping("/handle")
@@ -169,32 +174,6 @@ public String toString() {
169174

170175
// Static builder methods
171176

172-
/**
173-
* Create a builder with the given method, url, and uri variables.
174-
* <p>URI Template variables are expanded using the given URI variables, if any.
175-
* @param method the HTTP method (GET, POST, etc)
176-
* @param url the URL
177-
* @param uriVariables the variables to expand in the template
178-
* @return the created builder
179-
*/
180-
public static BodyBuilder method(HttpMethod method, String url, Object... uriVariables) {
181-
URI expanded = new UriTemplate(url).expand(uriVariables);
182-
return new DefaultBodyBuilder(method, expanded);
183-
}
184-
185-
/**
186-
* Create a builder with the given method, url, and uri variables.
187-
* <p>URI Template variables are expanded using the given URI variables, if any.
188-
* @param method the HTTP method (GET, POST, etc)
189-
* @param url the URL
190-
* @param uriVariables the variables to expand in the template
191-
* @return the created builder
192-
*/
193-
public static BodyBuilder method(HttpMethod method, String url, Map<String, ?> uriVariables) {
194-
URI expanded = new UriTemplate(url).expand(uriVariables);
195-
return new DefaultBodyBuilder(method, expanded);
196-
}
197-
198177
/**
199178
* Create a builder with the given method and url.
200179
* @param method the HTTP method (GET, POST, etc)
@@ -205,60 +184,15 @@ public static BodyBuilder method(HttpMethod method, URI url) {
205184
return new DefaultBodyBuilder(method, url);
206185
}
207186

208-
/**
209-
* Create a GET builder with the given url and uri variables.
210-
* <p>URI Template variables are expanded using the given URI variables, if any.
211-
* @param url the URL
212-
* @param uriVariables the variables to expand in the template
213-
* @return the created builder
214-
*/
215-
public static HeadersBuilder<?> get(String url, Object... uriVariables) {
216-
return method(HttpMethod.GET, url, uriVariables);
217-
}
218-
219-
/**
220-
* Create an HTTP GET builder with the given url and uri variables.
221-
* <p>URI Template variables are expanded using the given URI variables, if any.
222-
* @param url the URL
223-
* @param uriVariables the variables to expand in the template
224-
* @return the created builder
225-
*/
226-
public static HeadersBuilder<?> get(String url, Map<String, ?> uriVariables) {
227-
return method(HttpMethod.GET, url, uriVariables);
228-
}
229-
230187
/**
231188
* Create an HTTP GET builder with the given url.
232-
* <p>URI Template variables are expanded using the given URI variables, if any.
233189
* @param url the URL
234190
* @return the created builder
235191
*/
236192
public static HeadersBuilder<?> get(URI url) {
237193
return method(HttpMethod.GET, url);
238194
}
239195

240-
/**
241-
* Create an HTTP HEAD builder with the given url and uri variables.
242-
* <p>URI Template variables are expanded using the given URI variables, if any.
243-
* @param url the URL
244-
* @param uriVariables the variables to expand in the template
245-
* @return the created builder
246-
*/
247-
public static HeadersBuilder<?> head(String url, Object... uriVariables) {
248-
return method(HttpMethod.HEAD, url, uriVariables);
249-
}
250-
251-
/**
252-
* Create an HTTP HEAD builder with the given url and uri variables.
253-
* <p>URI Template variables are expanded using the given URI variables, if any.
254-
* @param url the URL
255-
* @param uriVariables the variables to expand in the template
256-
* @return the created builder
257-
*/
258-
public static HeadersBuilder<?> head(String url, Map<String, ?> uriVariables) {
259-
return method(HttpMethod.HEAD, url, uriVariables);
260-
}
261-
262196
/**
263197
* Create an HTTP HEAD builder with the given url.
264198
* @param url the URL
@@ -268,28 +202,6 @@ public static HeadersBuilder<?> head(URI url) {
268202
return method(HttpMethod.HEAD, url);
269203
}
270204

271-
/**
272-
* Create an HTTP POST builder with the given url and uri variables.
273-
* <p>URI Template variables are expanded using the given URI variables, if any.
274-
* @param url the URL
275-
* @param uriVariables the variables to expand in the template
276-
* @return the created builder
277-
*/
278-
public static BodyBuilder post(String url, Object... uriVariables) {
279-
return method(HttpMethod.POST, url, uriVariables);
280-
}
281-
282-
/**
283-
* Create an HTTP POST builder with the given url and uri variables.
284-
* <p>URI Template variables are expanded using the given URI variables, if any.
285-
* @param url the URL
286-
* @param uriVariables the variables to expand in the template
287-
* @return the created builder
288-
*/
289-
public static BodyBuilder post(String url, Map<String, ?> uriVariables) {
290-
return method(HttpMethod.POST, url, uriVariables);
291-
}
292-
293205
/**
294206
* Create an HTTP POST builder with the given url.
295207
* @param url the URL
@@ -299,28 +211,6 @@ public static BodyBuilder post(URI url) {
299211
return method(HttpMethod.POST, url);
300212
}
301213

302-
/**
303-
* Create an HTTP PUT builder with the given url and uri variables.
304-
* <p>URI Template variables are expanded using the given URI variables, if any.
305-
* @param url the URL
306-
* @param uriVariables the variables to expand in the template
307-
* @return the created builder
308-
*/
309-
public static BodyBuilder put(String url, Object... uriVariables) {
310-
return method(HttpMethod.PUT, url, uriVariables);
311-
}
312-
313-
/**
314-
* Create an HTTP PUT builder with the given url and uri variables.
315-
* <p>URI Template variables are expanded using the given URI variables, if any.
316-
* @param url the URL
317-
* @param uriVariables the variables to expand in the template
318-
* @return the created builder
319-
*/
320-
public static BodyBuilder put(String url, Map<String, ?> uriVariables) {
321-
return method(HttpMethod.PUT, url, uriVariables);
322-
}
323-
324214
/**
325215
* Create an HTTP PUT builder with the given url.
326216
* @param url the URL
@@ -330,28 +220,6 @@ public static BodyBuilder put(URI url) {
330220
return method(HttpMethod.PUT, url);
331221
}
332222

333-
/**
334-
* Create an HTTP PATCH builder with the given url and uri variables.
335-
* <p>URI Template variables are expanded using the given URI variables, if any.
336-
* @param url the URL
337-
* @param uriVariables the variables to expand in the template
338-
* @return the created builder
339-
*/
340-
public static BodyBuilder patch(String url, Object... uriVariables) {
341-
return method(HttpMethod.PATCH, url, uriVariables);
342-
}
343-
344-
/**
345-
* Create an HTTP PATCH builder with the given url and uri variables.
346-
* <p>URI Template variables are expanded using the given URI variables, if any.
347-
* @param url the URL
348-
* @param uriVariables the variables to expand in the template
349-
* @return the created builder
350-
*/
351-
public static BodyBuilder patch(String url, Map<String, ?> uriVariables) {
352-
return method(HttpMethod.PATCH, url, uriVariables);
353-
}
354-
355223
/**
356224
* Create an HTTP PATCH builder with the given url.
357225
* @param url the URL
@@ -361,28 +229,6 @@ public static BodyBuilder patch(URI url) {
361229
return method(HttpMethod.PATCH, url);
362230
}
363231

364-
/**
365-
* Create an HTTP DELETE builder with the given url and uri variables.
366-
* <p>URI Template variables are expanded using the given URI variables, if any.
367-
* @param url the URL
368-
* @param uriVariables the variables to expand in the template
369-
* @return the created builder
370-
*/
371-
public static HeadersBuilder<?> delete(String url, Object... uriVariables) {
372-
return method(HttpMethod.DELETE, url, uriVariables);
373-
}
374-
375-
/**
376-
* Create an HTTP DELETE builder with the given url and uri variables.
377-
* <p>URI Template variables are expanded using the given URI variables, if any.
378-
* @param url the URL
379-
* @param uriVariables the variables to expand in the template
380-
* @return the created builder
381-
*/
382-
public static HeadersBuilder<?> delete(String url, Map<String, ?> uriVariables) {
383-
return method(HttpMethod.DELETE, url, uriVariables);
384-
}
385-
386232
/**
387233
* Create an HTTP DELETE builder with the given url.
388234
* @param url the URL
@@ -392,28 +238,6 @@ public static HeadersBuilder<?> delete(URI url) {
392238
return method(HttpMethod.DELETE, url);
393239
}
394240

395-
/**
396-
* Create an HTTP OPTIONS builder with the given url and uri variables.
397-
* <p>URI Template variables are expanded using the given URI variables, if any.
398-
* @param url the URL
399-
* @param uriVariables the variables to expand in the template
400-
* @return the created builder
401-
*/
402-
public static HeadersBuilder<?> options(String url, Object... uriVariables) {
403-
return method(HttpMethod.OPTIONS, url, uriVariables);
404-
}
405-
406-
/**
407-
* Creates an HTTP OPTIONS builder with the given url and uri variables.
408-
* <p>URI Template variables are expanded using the given URI variables, if any.
409-
* @param url the URL
410-
* @param uriVariables the variables to expand in the template
411-
* @return the created builder
412-
*/
413-
public static HeadersBuilder<?> options(String url, Map<String, ?> uriVariables) {
414-
return method(HttpMethod.OPTIONS, url, uriVariables);
415-
}
416-
417241
/**
418242
* Creates an HTTP OPTIONS builder with the given url.
419243
* @param url the URL

spring-web/src/test/java/org/springframework/http/RequestEntityTests.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25-
import static org.junit.Assert.*;
26-
2725
import org.junit.Test;
2826

27+
import org.springframework.web.util.UriTemplate;
28+
29+
import static org.junit.Assert.*;
2930

3031
/**
3132
* Unit tests for {@link org.springframework.http.RequestEntity}.
33+
*
3234
* @author Arjen Poutsma
3335
*/
3436
public class RequestEntityTests {
3537

36-
3738
@Test
3839
public void normal() throws URISyntaxException {
3940
String headerName = "My-Custom-Header";
@@ -54,22 +55,24 @@ public void normal() throws URISyntaxException {
5455

5556
@Test
5657
public void uriVariablesExpansion() throws URISyntaxException {
57-
RequestEntity.get("http://example.com/{foo}", "bar").accept(MediaType.TEXT_PLAIN).build();
58+
URI uri = new UriTemplate("http://example.com/{foo}").expand("bar");
59+
RequestEntity.get(uri).accept(MediaType.TEXT_PLAIN).build();
5860

5961
String url = "http://www.{host}.com/{path}";
6062
String host = "example";
6163
String path = "foo/bar";
62-
6364
URI expected = new URI("http://www.example.com/foo/bar");
6465

65-
RequestEntity<?> entity = RequestEntity.method(HttpMethod.GET, url, host, path).build();
66+
uri = new UriTemplate(url).expand(host, path);
67+
RequestEntity<?> entity = RequestEntity.get(uri).build();
6668
assertEquals(expected, entity.getUrl());
6769

6870
Map<String, String> uriVariables = new HashMap<>(2);
6971
uriVariables.put("host", host);
7072
uriVariables.put("path", path);
7173

72-
entity = RequestEntity.method(HttpMethod.GET, url, uriVariables).build();
74+
uri = new UriTemplate(url).expand(uriVariables);
75+
entity = RequestEntity.get(uri).build();
7376
assertEquals(expected, entity.getUrl());
7477
}
7578

@@ -94,7 +97,7 @@ public void headers() throws URISyntaxException {
9497
long contentLength = 67890;
9598
MediaType contentType = MediaType.TEXT_PLAIN;
9699

97-
RequestEntity<Void> responseEntity = RequestEntity.post("http://example.com").
100+
RequestEntity<Void> responseEntity = RequestEntity.post(new URI("http://example.com")).
98101
accept(accept).
99102
acceptCharset(charset).
100103
ifModifiedSince(ifModifiedSince).

0 commit comments

Comments
 (0)