Allow to change URL/method in ClientRequest.Builder

This commit exposes the ClientRequest's URL and HttpMethod fields via a
setter, so that they can be changed more easily in a request that was
created via ClientRequest.from(ClientRequest).

Issue: SPR-16093
This commit is contained in:
Arjen Poutsma
2017-10-23 16:39:33 +02:00
parent 585ad5961a
commit 40a6fba443
3 changed files with 48 additions and 7 deletions

View File

@@ -134,6 +134,20 @@ public interface ClientRequest {
*/
interface Builder {
/**
* Set the method of the request.
* @param method the new method
* @return this builder
*/
Builder method(HttpMethod method);
/**
* Set the url of the request.
* @param url the new url
* @return this builder
*/
Builder url(URI url);
/**
* Add the given header value(s) under the given name.
* @param headerName the header name

View File

@@ -49,16 +49,16 @@ import org.springframework.web.reactive.function.BodyInserters;
*/
class DefaultClientRequestBuilder implements ClientRequest.Builder {
private final HttpMethod method;
private final URI url;
private final HttpHeaders headers = new HttpHeaders();
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
private final Map<String, Object> attributes = new LinkedHashMap<>();
private HttpMethod method;
private URI url;
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
@@ -67,6 +67,19 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder {
this.url = url;
}
@Override
public ClientRequest.Builder method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null");
this.method = method;
return this;
}
@Override
public ClientRequest.Builder url(URI url) {
Assert.notNull(url, "'url' must not be null");
this.url = url;
return this;
}
@Override
public ClientRequest.Builder header(String headerName, String... headerValues) {