Skip to content

Commit fc12891

Browse files
committed
Add ServerRequest::firstHeader
This commit introduces the method firstHeaderiin both WebMvc.fn and WebFlux.fn, which return the first header value of a given header name, if any.
1 parent b92515b commit fc12891

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -371,6 +371,18 @@ interface Headers {
371371
*/
372372
List<String> header(String headerName);
373373

374+
/**
375+
* Get the first header value, if any, for the header for the given name.
376+
* <p>Returns {@code null} if no header values are found.
377+
* @param headerName the header name
378+
* @since 5.2.5
379+
*/
380+
@Nullable
381+
default String firstHeader(String headerName) {
382+
List<String> list = header(headerName);
383+
return list.isEmpty() ? null : list.get(0);
384+
}
385+
374386
/**
375387
* Get the headers as an instance of {@link HttpHeaders}.
376388
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -208,6 +208,8 @@ public void header() {
208208
assertThat(headers.acceptCharset()).isEqualTo(acceptCharset);
209209
assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength));
210210
assertThat(headers.contentType()).isEqualTo(Optional.of(contentType));
211+
assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
212+
assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
211213
assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders);
212214
}
213215

spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -307,6 +307,18 @@ interface Headers {
307307
*/
308308
List<String> header(String headerName);
309309

310+
/**
311+
* Get the first header value, if any, for the header for the given name.
312+
* <p>Returns {@code null} if no header values are found.
313+
* @param headerName the header name
314+
* @since 5.2.5
315+
*/
316+
@Nullable
317+
default String firstHeader(String headerName) {
318+
List<String> list = header(headerName);
319+
return list.isEmpty() ? null : list.get(0);
320+
}
321+
310322
/**
311323
* Get the headers as an instance of {@link HttpHeaders}.
312324
*/

spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerRequestTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -209,6 +209,8 @@ public void header() {
209209
assertThat(headers.acceptCharset()).isEqualTo(acceptCharset);
210210
assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength));
211211
assertThat(headers.contentType()).isEqualTo(Optional.of(contentType));
212+
assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
213+
assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
212214
assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders);
213215
}
214216

0 commit comments

Comments
 (0)