diff --git a/spring-web/src/main/java/org/springframework/http/RequestEntity.java b/spring-web/src/main/java/org/springframework/http/RequestEntity.java index eb20ad4981..d03fb08025 100644 --- a/spring-web/src/main/java/org/springframework/http/RequestEntity.java +++ b/spring-web/src/main/java/org/springframework/http/RequestEntity.java @@ -19,12 +19,10 @@ package org.springframework.http; import java.net.URI; import java.nio.charset.Charset; import java.util.Arrays; -import java.util.Map; import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; -import org.springframework.web.util.UriTemplate; /** * Extension of {@link HttpEntity} that adds a {@linkplain HttpMethod method} and @@ -35,10 +33,17 @@ import org.springframework.web.util.UriTemplate; * {@link org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange()}: *
  * MyRequest body = ...
- * RequestEntity<MyRequest> request = RequestEntity.post("http://example.com/{foo}", "bar").accept(MediaType.APPLICATION_JSON).body(body);
+ * RequestEntity<MyRequest> request = RequestEntity.post(new URI("http://example.com/bar").accept(MediaType.APPLICATION_JSON).body(body);
  * ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);
  * 
* + *

If you would like to provide a URI template with variables, consider using + * {@link org.springframework.web.util.UriTemplate}: + *

+ * URI uri = new UriTemplate("http://example.com/{foo}"").expand("bar");
+ * RequestEntity<MyRequest> request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
+ * 
+ * *

Can also be used in Spring MVC, as a parameter in a @Controller method: *

  * @RequestMapping("/handle")
@@ -169,32 +174,6 @@ public class RequestEntity extends HttpEntity {
 
 	// Static builder methods
 
-	/**
-	 * Create a builder with the given method, url, and uri variables.
-	 * 

URI Template variables are expanded using the given URI variables, if any. - * @param method the HTTP method (GET, POST, etc) - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder method(HttpMethod method, String url, Object... uriVariables) { - URI expanded = new UriTemplate(url).expand(uriVariables); - return new DefaultBodyBuilder(method, expanded); - } - - /** - * Create a builder with the given method, url, and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param method the HTTP method (GET, POST, etc) - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder method(HttpMethod method, String url, Map uriVariables) { - URI expanded = new UriTemplate(url).expand(uriVariables); - return new DefaultBodyBuilder(method, expanded); - } - /** * Create a builder with the given method and url. * @param method the HTTP method (GET, POST, etc) @@ -205,31 +184,8 @@ public class RequestEntity extends HttpEntity { return new DefaultBodyBuilder(method, url); } - /** - * Create a GET builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder get(String url, Object... uriVariables) { - return method(HttpMethod.GET, url, uriVariables); - } - - /** - * Create an HTTP GET builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder get(String url, Map uriVariables) { - return method(HttpMethod.GET, url, uriVariables); - } - /** * Create an HTTP GET builder with the given url. - *

URI Template variables are expanded using the given URI variables, if any. * @param url the URL * @return the created builder */ @@ -237,28 +193,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.GET, url); } - /** - * Create an HTTP HEAD builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder head(String url, Object... uriVariables) { - return method(HttpMethod.HEAD, url, uriVariables); - } - - /** - * Create an HTTP HEAD builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder head(String url, Map uriVariables) { - return method(HttpMethod.HEAD, url, uriVariables); - } - /** * Create an HTTP HEAD builder with the given url. * @param url the URL @@ -268,28 +202,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.HEAD, url); } - /** - * Create an HTTP POST builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder post(String url, Object... uriVariables) { - return method(HttpMethod.POST, url, uriVariables); - } - - /** - * Create an HTTP POST builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder post(String url, Map uriVariables) { - return method(HttpMethod.POST, url, uriVariables); - } - /** * Create an HTTP POST builder with the given url. * @param url the URL @@ -299,28 +211,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.POST, url); } - /** - * Create an HTTP PUT builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder put(String url, Object... uriVariables) { - return method(HttpMethod.PUT, url, uriVariables); - } - - /** - * Create an HTTP PUT builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder put(String url, Map uriVariables) { - return method(HttpMethod.PUT, url, uriVariables); - } - /** * Create an HTTP PUT builder with the given url. * @param url the URL @@ -330,28 +220,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.PUT, url); } - /** - * Create an HTTP PATCH builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder patch(String url, Object... uriVariables) { - return method(HttpMethod.PATCH, url, uriVariables); - } - - /** - * Create an HTTP PATCH builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static BodyBuilder patch(String url, Map uriVariables) { - return method(HttpMethod.PATCH, url, uriVariables); - } - /** * Create an HTTP PATCH builder with the given url. * @param url the URL @@ -361,28 +229,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.PATCH, url); } - /** - * Create an HTTP DELETE builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder delete(String url, Object... uriVariables) { - return method(HttpMethod.DELETE, url, uriVariables); - } - - /** - * Create an HTTP DELETE builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder delete(String url, Map uriVariables) { - return method(HttpMethod.DELETE, url, uriVariables); - } - /** * Create an HTTP DELETE builder with the given url. * @param url the URL @@ -392,28 +238,6 @@ public class RequestEntity extends HttpEntity { return method(HttpMethod.DELETE, url); } - /** - * Create an HTTP OPTIONS builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder options(String url, Object... uriVariables) { - return method(HttpMethod.OPTIONS, url, uriVariables); - } - - /** - * Creates an HTTP OPTIONS builder with the given url and uri variables. - *

URI Template variables are expanded using the given URI variables, if any. - * @param url the URL - * @param uriVariables the variables to expand in the template - * @return the created builder - */ - public static HeadersBuilder options(String url, Map uriVariables) { - return method(HttpMethod.OPTIONS, url, uriVariables); - } - /** * Creates an HTTP OPTIONS builder with the given url. * @param url the URL diff --git a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java index 84f2f4783b..7409c412f3 100644 --- a/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/RequestEntityTests.java @@ -22,18 +22,19 @@ import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; - import org.junit.Test; +import org.springframework.web.util.UriTemplate; + +import static org.junit.Assert.*; /** * Unit tests for {@link org.springframework.http.RequestEntity}. + * * @author Arjen Poutsma */ public class RequestEntityTests { - @Test public void normal() throws URISyntaxException { String headerName = "My-Custom-Header"; @@ -54,22 +55,24 @@ public class RequestEntityTests { @Test public void uriVariablesExpansion() throws URISyntaxException { - RequestEntity.get("http://example.com/{foo}", "bar").accept(MediaType.TEXT_PLAIN).build(); + URI uri = new UriTemplate("http://example.com/{foo}").expand("bar"); + RequestEntity.get(uri).accept(MediaType.TEXT_PLAIN).build(); String url = "http://www.{host}.com/{path}"; String host = "example"; String path = "foo/bar"; - URI expected = new URI("http://www.example.com/foo/bar"); - RequestEntity entity = RequestEntity.method(HttpMethod.GET, url, host, path).build(); + uri = new UriTemplate(url).expand(host, path); + RequestEntity entity = RequestEntity.get(uri).build(); assertEquals(expected, entity.getUrl()); Map uriVariables = new HashMap<>(2); uriVariables.put("host", host); uriVariables.put("path", path); - entity = RequestEntity.method(HttpMethod.GET, url, uriVariables).build(); + uri = new UriTemplate(url).expand(uriVariables); + entity = RequestEntity.get(uri).build(); assertEquals(expected, entity.getUrl()); } @@ -94,7 +97,7 @@ public class RequestEntityTests { long contentLength = 67890; MediaType contentType = MediaType.TEXT_PLAIN; - RequestEntity responseEntity = RequestEntity.post("http://example.com"). + RequestEntity responseEntity = RequestEntity.post(new URI("http://example.com")). accept(accept). acceptCharset(charset). ifModifiedSince(ifModifiedSince).