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
This commit is contained in:
@@ -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()}:
|
||||
* <pre class="code">
|
||||
* 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);
|
||||
* </pre>
|
||||
*
|
||||
* <p>If you would like to provide a URI template with variables, consider using
|
||||
* {@link org.springframework.web.util.UriTemplate}:
|
||||
* <pre class="code">
|
||||
* URI uri = new UriTemplate("http://example.com/{foo}"").expand("bar");
|
||||
* RequestEntity<MyRequest> request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
|
||||
* </pre>
|
||||
*
|
||||
* <p>Can also be used in Spring MVC, as a parameter in a @Controller method:
|
||||
* <pre class="code">
|
||||
* @RequestMapping("/handle")
|
||||
@@ -169,32 +174,6 @@ public class RequestEntity<T> extends HttpEntity<T> {
|
||||
|
||||
// Static builder methods
|
||||
|
||||
/**
|
||||
* Create a builder with the given method, url, and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return new DefaultBodyBuilder(method, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a GET builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> uriVariables) {
|
||||
return method(HttpMethod.GET, url, uriVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP GET builder with the given url.
|
||||
* <p>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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.GET, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP HEAD builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.HEAD, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP POST builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.POST, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP PUT builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.PUT, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP PATCH builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.PATCH, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP DELETE builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> 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<T> extends HttpEntity<T> {
|
||||
return method(HttpMethod.DELETE, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP OPTIONS builder with the given url and uri variables.
|
||||
* <p>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.
|
||||
* <p>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<String, ?> uriVariables) {
|
||||
return method(HttpMethod.OPTIONS, url, uriVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HTTP OPTIONS builder with the given url.
|
||||
* @param url the URL
|
||||
|
||||
@@ -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<String, String> 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<Void> responseEntity = RequestEntity.post("http://example.com").
|
||||
RequestEntity<Void> responseEntity = RequestEntity.post(new URI("http://example.com")).
|
||||
accept(accept).
|
||||
acceptCharset(charset).
|
||||
ifModifiedSince(ifModifiedSince).
|
||||
|
||||
Reference in New Issue
Block a user