diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java index 4bca08b4544..56f98e19b39 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -371,6 +371,18 @@ public interface ServerRequest { */ List header(String headerName); + /** + * Get the first header value, if any, for the header for the given name. + *

Returns {@code null} if no header values are found. + * @param headerName the header name + * @since 5.2.5 + */ + @Nullable + default String firstHeader(String headerName) { + List list = header(headerName); + return list.isEmpty() ? null : list.get(0); + } + /** * Get the headers as an instance of {@link HttpHeaders}. */ diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java index b4e17cca692..2ebd457f0c4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -208,6 +208,8 @@ public class DefaultServerRequestTests { assertThat(headers.acceptCharset()).isEqualTo(acceptCharset); assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength)); assertThat(headers.contentType()).isEqualTo(Optional.of(contentType)); + assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE); + assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE); assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java index 2f7fb62724e..b292c6c2722 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -307,6 +307,18 @@ public interface ServerRequest { */ List header(String headerName); + /** + * Get the first header value, if any, for the header for the given name. + *

Returns {@code null} if no header values are found. + * @param headerName the header name + * @since 5.2.5 + */ + @Nullable + default String firstHeader(String headerName) { + List list = header(headerName); + return list.isEmpty() ? null : list.get(0); + } + /** * Get the headers as an instance of {@link HttpHeaders}. */ diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerRequestTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerRequestTests.java index 58d8289b17e..7ba69dd0a82 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerRequestTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -209,6 +209,8 @@ public class DefaultServerRequestTests { assertThat(headers.acceptCharset()).isEqualTo(acceptCharset); assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength)); assertThat(headers.contentType()).isEqualTo(Optional.of(contentType)); + assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE); + assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE); assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders); }