diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index da71fbe565a1..2d406d39d46c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -362,9 +362,7 @@ private void writeStatusAndHeaders(ServerHttpResponse response) { private static void copy(MultiValueMap src, MultiValueMap dst) { if (!src.isEmpty()) { - src.entrySet().stream() - .filter(entry -> !dst.containsKey(entry.getKey())) - .forEach(entry -> dst.put(entry.getKey(), entry.getValue())); + dst.putAll(src); } } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java index 38b295463ca4..c3780d2c443d 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java @@ -320,6 +320,23 @@ public void copyCookies() { assertThat(serverResponse.block().cookies().isEmpty()).isFalse(); } + @Test + public void overwriteHeaders() { + ServerResponse serverResponse = + ServerResponse.ok().headers(headers -> headers.set("Foo", "Bar")).build().block(); + assertThat(serverResponse).isNotNull(); + + MockServerWebExchange mockExchange = MockServerWebExchange + .builder(MockServerHttpRequest.get("https://example.org")) + .build(); + MockServerHttpResponse response = mockExchange.getResponse(); + response.getHeaders().set("Foo", "Baz"); + + serverResponse.writeTo(mockExchange, EMPTY_CONTEXT).block(); + + assertThat(response.getHeaders().getFirst("Foo")).isEqualTo("Bar"); + } + @Test public void build() {