Skip to content

Commit 424bc75

Browse files
committed
Remove headers(HttpHeaders)
This commit removes the headers(HttpHeaders) method on ClientRequest and ServerResponse, in favor of headers(Consumer<HttpHeaders>), which is more flexible.
1 parent 9bf82dc commit 424bc75

File tree

6 files changed

+23
-52
lines changed

6 files changed

+23
-52
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public interface ClientRequest {
8888
static Builder from(ClientRequest other) {
8989
Assert.notNull(other, "'other' must not be null");
9090
return new DefaultClientRequestBuilder(other.method(), other.url())
91-
.headers(other.headers())
92-
.cookies(other.cookies())
91+
.headers(headers -> headers.addAll(other.headers()))
92+
.cookies(cookies -> cookies.addAll(other.cookies()))
9393
.body(other.body());
9494
}
9595

@@ -118,13 +118,6 @@ interface Builder {
118118
*/
119119
Builder header(String headerName, String... headerValues);
120120

121-
/**
122-
* Add the given headers into this request's headers map.
123-
* @param headers the existing HttpHeaders to add from
124-
* @return this builder
125-
*/
126-
Builder headers(HttpHeaders headers);
127-
128121
/**
129122
* Manipulate this request's headers with the given consumer. The
130123
* headers provided to the consumer are "live", so that the consumer can be used to
@@ -144,13 +137,6 @@ interface Builder {
144137
*/
145138
Builder cookie(String name, String... values);
146139

147-
/**
148-
* Add the given cookies into this request's cookies map.
149-
* @param cookies the existing cookies to copy from
150-
* @return this builder
151-
*/
152-
Builder cookies(MultiValueMap<String, String> cookies);
153-
154140
/**
155141
* Manipulate this request's cookies with the given consumer. The
156142
* map provided to the consumer is "live", so that the consumer can be used to

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ public ClientRequest.Builder header(String headerName, String... headerValues) {
7272
return this;
7373
}
7474

75-
@Override
76-
public ClientRequest.Builder headers(HttpHeaders headers) {
77-
Assert.notNull(headers, "'headers' must not be null");
78-
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
79-
String headerName = entry.getKey();
80-
for (String headerValue : entry.getValue()) {
81-
this.headers.add(headerName, headerValue);
82-
}
83-
}
84-
return this;
85-
}
86-
8775
@Override
8876
public ClientRequest.Builder headers(Consumer<HttpHeaders> headersConsumer) {
8977
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
@@ -99,18 +87,6 @@ public ClientRequest.Builder cookie(String name, String... values) {
9987
return this;
10088
}
10189

102-
@Override
103-
public ClientRequest.Builder cookies(MultiValueMap<String, String> cookies) {
104-
Assert.notNull(cookies, "'cookies' must not be null");
105-
for (Map.Entry<String, List<String>> entry : cookies.entrySet()) {
106-
String cookieName = entry.getKey();
107-
for (String cookieValue : entry.getValue()) {
108-
this.cookies.add(cookieName, cookieValue);
109-
}
110-
}
111-
return this;
112-
}
113-
11490
@Override
11591
public ClientRequest.Builder cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) {
11692
Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null");

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.time.ZonedDateTime;
2323
import java.time.format.DateTimeFormatter;
2424
import java.util.Arrays;
25-
import java.util.Collections;
2625
import java.util.List;
2726
import java.util.Map;
2827
import java.util.function.Function;
@@ -292,7 +291,9 @@ public Mono<ClientResponse> exchange() {
292291
}
293292

294293
private ClientRequest.Builder initRequestBuilder() {
295-
return ClientRequest.method(this.httpMethod, this.uri).headers(initHeaders()).cookies(initCookies());
294+
return ClientRequest.method(this.httpMethod, this.uri)
295+
.headers(headers -> headers.addAll(initHeaders()))
296+
.cookies(cookies -> cookies.addAll(initCookies()));
296297
}
297298

298299
private HttpHeaders initHeaders() {

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Optional;
2929
import java.util.Set;
3030
import java.util.function.BiFunction;
31+
import java.util.function.Consumer;
3132

3233
import org.reactivestreams.Publisher;
3334
import reactor.core.publisher.Mono;
@@ -74,8 +75,9 @@ public ServerResponse.BodyBuilder header(String headerName, String... headerValu
7475
}
7576

7677
@Override
77-
public ServerResponse.BodyBuilder headers(HttpHeaders headers) {
78-
this.headers.putAll(headers);
78+
public ServerResponse.BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
79+
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
80+
headersConsumer.accept(this.headers);
7981
return this;
8082
}
8183

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.Set;
2525
import java.util.function.BiFunction;
26+
import java.util.function.Consumer;
2627

2728
import org.reactivestreams.Publisher;
2829
import reactor.core.publisher.Mono;
@@ -83,7 +84,7 @@ public interface ServerResponse {
8384
static BodyBuilder from(ServerResponse other) {
8485
Assert.notNull(other, "Other ServerResponse must not be null");
8586
DefaultServerResponseBuilder builder = new DefaultServerResponseBuilder(other.statusCode());
86-
return builder.headers(other.headers());
87+
return builder.headers(headers -> headers.addAll(other.headers()));
8788
}
8889

8990
/**
@@ -207,12 +208,15 @@ interface HeadersBuilder<B extends HeadersBuilder<B>> {
207208
B header(String headerName, String... headerValues);
208209

209210
/**
210-
* Copy the given headers into the entity's headers map.
211-
* @param headers the existing HttpHeaders to copy from
211+
* Manipulate this response's headers with the given consumer. The
212+
* headers provided to the consumer are "live", so that the consumer can be used to
213+
* {@linkplain HttpHeaders#set(String, String) overwrite} existing header values,
214+
* {@linkplain HttpHeaders#remove(Object) remove} values, or use any of the other
215+
* {@link HttpHeaders} methods.
216+
* @param headersConsumer a function that consumes the {@code HttpHeaders}
212217
* @return this builder
213-
* @see HttpHeaders#add(String, String)
214218
*/
215-
B headers(HttpHeaders headers);
219+
B headers(Consumer<HttpHeaders> headersConsumer);
216220

217221
/**
218222
* Set the set of allowed {@link HttpMethod HTTP methods}, as specified

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,12 @@ public void statusCode() throws Exception {
253253

254254
@Test
255255
public void headers() throws Exception {
256-
HttpHeaders headers = new HttpHeaders();
257-
Mono<ServerResponse> result = ServerResponse.ok().headers(headers).build();
256+
HttpHeaders newHeaders = new HttpHeaders();
257+
newHeaders.set("foo", "bar");
258+
Mono<ServerResponse> result =
259+
ServerResponse.ok().headers(headers -> headers.addAll(newHeaders)).build();
258260
StepVerifier.create(result)
259-
.expectNextMatches(response -> headers.equals(response.headers()))
261+
.expectNextMatches(response -> newHeaders.equals(response.headers()))
260262
.expectComplete()
261263
.verify();
262264

0 commit comments

Comments
 (0)