#24406 - Add String based URI template variants

This commit is contained in:
Parviz ROzikov 2020-01-21 01:03:43 +07:00 committed by Rossen Stoyanchev
parent d1708460e7
commit 816466e492
3 changed files with 217 additions and 6 deletions

View File

@ -25,12 +25,18 @@ import java.util.Arrays;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriTemplateHandler;
/** /**
* Extension of {@link HttpEntity} that adds a {@linkplain HttpMethod method} and * Extension of {@link HttpEntity} that adds a {@linkplain HttpMethod method} and
* {@linkplain URI uri}. Used in {@code RestTemplate} and {@code @Controller} methods. * {@linkplain URI uri} or String based uri template with placeholder. Uri variables can be provided
* through the builder {@link HeadersBuilder#uriVariables(Object...)}.
*
* Used in {@code RestTemplate} and {@code @Controller} methods.
* *
* <p>In {@code RestTemplate}, this class is used as parameter in * <p>In {@code RestTemplate}, this class is used as parameter in
* {@link org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange()}: * {@link org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange()}:
@ -54,6 +60,19 @@ import org.springframework.util.ObjectUtils;
* RequestEntity&lt;MyRequest&gt; request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body); * RequestEntity&lt;MyRequest&gt; request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
* </pre> * </pre>
* *
* <p> if you would like to provide a string base URI template with variable, consider using
* {@link RequestEntity#method(HttpMethod, String)}}, {@link RequestEntity#get(String)},
* {@link RequestEntity#post(String)}, {@link RequestEntity#put(String)}, {@link RequestEntity#delete(String)},
* {@link RequestEntity#patch(String)}, {@link RequestEntity#options(String)}
*
* <pre class="code>
* RequestEntity request = RequestEntity
* .post(&quot;https://example.com/{foo}&quot;)
* .uriVariables(&quot;bar&quot;)
* .accept(MediaType.APPLICATION_JSON)
* .build();
* </pre>
*
* <p>Can also be used in Spring MVC, as a parameter in a @Controller method: * <p>Can also be used in Spring MVC, as a parameter in a @Controller method:
* <pre class="code"> * <pre class="code">
* &#64;RequestMapping("/handle") * &#64;RequestMapping("/handle")
@ -66,6 +85,7 @@ import org.springframework.util.ObjectUtils;
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Parviz Rozikov
* @since 4.1 * @since 4.1
* @param <T> the body type * @param <T> the body type
* @see #getMethod() * @see #getMethod()
@ -73,11 +93,20 @@ import org.springframework.util.ObjectUtils;
*/ */
public class RequestEntity<T> extends HttpEntity<T> { public class RequestEntity<T> extends HttpEntity<T> {
private final static UriTemplateHandler DEFAULT_URI_BUILDER_FACTORY = new DefaultUriBuilderFactory();
@Nullable @Nullable
private final HttpMethod method; private final HttpMethod method;
@Nullable
private final URI url; private final URI url;
@Nullable
private String uri;
@Nullable
private Object[] uriVariables;
@Nullable @Nullable
private final Type type; private final Type type;
@ -154,6 +183,24 @@ public class RequestEntity<T> extends HttpEntity<T> {
this.type = type; this.type = type;
} }
/**
* Private Constructor with method, URL, UriTemplate and varargs urivariables but without body nor headers.
* @param method the method
* @param url the URL
* @param uri the UriTemplate
* @param uriVariables the uriVariables
*/
private RequestEntity(MultiValueMap<String, String> headers, HttpMethod method, @Nullable URI url,
@Nullable String uri, @Nullable Object... uriVariables) {
super(null, headers);
Assert.isTrue(uri == null || url == null, "Either url or url must be not null");
this.method = method;
this.url = url;
this.type = null;
this.uri = uri;
this.uriVariables = uriVariables;
}
/** /**
* Return the HTTP method of the request. * Return the HTTP method of the request.
@ -166,11 +213,29 @@ public class RequestEntity<T> extends HttpEntity<T> {
/** /**
* Return the URL of the request. * Return the URL of the request.
* Used {@link org.springframework.web.util.DefaultUriBuilderFactory} to expand and
* encode {@link DefaultUriBuilderFactory#setEncodingMode} when provided {@link RequestEntity#uri}
* @return the URL as a {@code URI} * @return the URL as a {@code URI}
*/ */
public URI getUrl() { public URI getUrl() {
if (uri == null) {
return this.url; return this.url;
} }
return DEFAULT_URI_BUILDER_FACTORY.expand(uri, uriVariables);
}
/**
* Return the URL of the request.
* @return the URL as a {@code URI}
* @since 5.3
*/
public URI getUrl(UriTemplateHandler uriTemplateHandler) {
if (uri == null) {
return this.url;
}
return uriTemplateHandler.expand(uri, uriVariables);
}
/** /**
* Return the type of the request's body. * Return the type of the request's body.
@ -241,6 +306,19 @@ public class RequestEntity<T> extends HttpEntity<T> {
return new DefaultBodyBuilder(method, url); return new DefaultBodyBuilder(method, url);
} }
/**
* Create a builder with the given method and given string base uri template.
* @param method the HTTP method (GET, POST, etc)
* @param uri the uri
* @return the created builder
* @see RequestEntity
* @since 5.3
*/
public static BodyBuilder method(HttpMethod method, String uri) {
return new DefaultBodyBuilder(method, uri);
}
/** /**
* Create an HTTP GET builder with the given url. * Create an HTTP GET builder with the given url.
* @param url the URL * @param url the URL
@ -250,6 +328,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.GET, url); return method(HttpMethod.GET, url);
} }
/**
* Create an HTTP GET builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> get(String uri) {
return method(HttpMethod.GET, uri);
}
/** /**
* Create an HTTP HEAD builder with the given url. * Create an HTTP HEAD builder with the given url.
* @param url the URL * @param url the URL
@ -259,6 +347,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.HEAD, url); return method(HttpMethod.HEAD, url);
} }
/**
* Create an HTTP HEAD builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> head(String uri) {
return method(HttpMethod.HEAD, uri);
}
/** /**
* Create an HTTP POST builder with the given url. * Create an HTTP POST builder with the given url.
* @param url the URL * @param url the URL
@ -268,6 +366,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.POST, url); return method(HttpMethod.POST, url);
} }
/**
* Create an HTTP POST builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static BodyBuilder post(String uri) {
return method(HttpMethod.POST, uri);
}
/** /**
* Create an HTTP PUT builder with the given url. * Create an HTTP PUT builder with the given url.
* @param url the URL * @param url the URL
@ -277,6 +385,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.PUT, url); return method(HttpMethod.PUT, url);
} }
/**
* Create an HTTP PUT builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static BodyBuilder put(String uri) {
return method(HttpMethod.PUT, uri);
}
/** /**
* Create an HTTP PATCH builder with the given url. * Create an HTTP PATCH builder with the given url.
* @param url the URL * @param url the URL
@ -286,6 +404,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.PATCH, url); return method(HttpMethod.PATCH, url);
} }
/**
* Create an HTTP PATCH builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static BodyBuilder patch(String uri) {
return method(HttpMethod.PATCH, uri);
}
/** /**
* Create an HTTP DELETE builder with the given url. * Create an HTTP DELETE builder with the given url.
* @param url the URL * @param url the URL
@ -295,6 +423,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.DELETE, url); return method(HttpMethod.DELETE, url);
} }
/**
* Create an HTTP DELETE builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> delete(String uri) {
return method(HttpMethod.DELETE, uri);
}
/** /**
* Creates an HTTP OPTIONS builder with the given url. * Creates an HTTP OPTIONS builder with the given url.
* @param url the URL * @param url the URL
@ -304,6 +442,16 @@ public class RequestEntity<T> extends HttpEntity<T> {
return method(HttpMethod.OPTIONS, url); return method(HttpMethod.OPTIONS, url);
} }
/**
* Creates an HTTP OPTIONS builder with the given string base uri template.
* @param uri the uri template
* @return the created builder
* @since 5.3
*/
public static HeadersBuilder<?> options(String uri) {
return method(HttpMethod.OPTIONS, uri);
}
/** /**
* Defines a builder that adds headers to the request entity. * Defines a builder that adds headers to the request entity.
@ -383,6 +531,13 @@ public class RequestEntity<T> extends HttpEntity<T> {
*/ */
B ifNoneMatch(String... ifNoneMatches); B ifNoneMatch(String... ifNoneMatches);
/**
* Set the values of the {@code If-None-Match} header.
* @param uriVariables the variables to expand the template
* @since 5.3
*/
B uriVariables(Object... uriVariables);
/** /**
* Builds the request entity with no body. * Builds the request entity with no body.
* @return the request entity * @return the request entity
@ -439,13 +594,28 @@ public class RequestEntity<T> extends HttpEntity<T> {
private final HttpMethod method; private final HttpMethod method;
@Nullable
private final URI url; private final URI url;
@Nullable
private final String uri;
@Nullable
private Object[] uriVariables;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
public DefaultBodyBuilder(HttpMethod method, URI url) { public DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method; this.method = method;
this.url = url; this.url = url;
this.uri = null;
}
public DefaultBodyBuilder(HttpMethod method, String uri) {
this.method = method;
this.uri = uri;
this.url = null;
} }
@Override @Override
@ -518,9 +688,18 @@ public class RequestEntity<T> extends HttpEntity<T> {
return this; return this;
} }
@Override
public BodyBuilder uriVariables(Object... uriVariables) {
this.uriVariables = uriVariables;
return this;
}
@Override @Override
public RequestEntity<Void> build() { public RequestEntity<Void> build() {
return new RequestEntity<>(this.headers, this.method, this.url); if (this.url != null){
new RequestEntity<>(this.headers, this.method, this.url);
}
return new RequestEntity<>(this.headers, this.method, this.url, uri, uriVariables);
} }
@Override @Override

View File

@ -638,7 +638,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType); RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return nonNull(doExecute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor)); return nonNull(doExecute(requestEntity.getUrl(this.uriTemplateHandler), requestEntity.getMethod(), requestCallback, responseExtractor));
} }
@Override @Override
@ -648,7 +648,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
Type type = responseType.getType(); Type type = responseType.getType();
RequestCallback requestCallback = httpEntityCallback(requestEntity, type); RequestCallback requestCallback = httpEntityCallback(requestEntity, type);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type);
return nonNull(doExecute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor)); return nonNull(doExecute(requestEntity.getUrl(this.uriTemplateHandler), requestEntity.getMethod(), requestCallback, responseExtractor));
} }

View File

@ -27,7 +27,10 @@ import java.util.Map;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
import org.springframework.web.util.UriTemplateHandler;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -35,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Unit tests for {@link org.springframework.http.RequestEntity}. * Unit tests for {@link org.springframework.http.RequestEntity}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Parviz Rozikov
*/ */
public class RequestEntityTests { public class RequestEntityTests {
@ -79,6 +83,34 @@ public class RequestEntityTests {
assertThat(entity.getUrl()).isEqualTo(expected); assertThat(entity.getUrl()).isEqualTo(expected);
} }
@Test
public void uriExpansion() throws URISyntaxException{
String template = "/foo?bar={bar}";
String bar = "foo";
URI expected = new URI("/foo?bar=foo");
RequestEntity<Void> entity = RequestEntity.get(template).uriVariables(bar).build();
assertThat(entity.getUrl()).isEqualTo(expected);
String url = "https://www.{host}.com/{path}";
String host = "example";
String path = "foo/bar";
expected = new URI("https://www.example.com/foo/bar");
entity = RequestEntity.get(url).uriVariables(host, path).build();
DefaultUriBuilderFactory uriTemplateHandler = new DefaultUriBuilderFactory();
uriTemplateHandler.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
assertThat(entity.getUrl(uriTemplateHandler)).isEqualTo(expected);
expected = new URI("https://www.example.com/foo%2Fbar");
uriTemplateHandler.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.TEMPLATE_AND_VALUES);
assertThat(entity.getUrl(uriTemplateHandler)).isEqualTo(expected);
}
@Test @Test
public void get() { public void get() {
RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("https://example.com")).accept( RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("https://example.com")).accept(