提交 f806594e 编写于 作者: S Sam Brannen

Introduce getOrEmpty(String) convenience method in HttpHeaders

This commit introduces a getOrEmpty(String) method in HttpHeaders that
returns an immutable, empty list if no values are present for the
specified header name. This is provided as a convenience over the
existing get(String) method which returns null in such cases.

Closes gh-22949
上级 9ca73541
...@@ -71,6 +71,7 @@ import org.springframework.util.StringUtils; ...@@ -71,6 +71,7 @@ import org.springframework.util.StringUtils;
* @author Brian Clozel * @author Brian Clozel
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Josh Long * @author Josh Long
* @author Sam Brannen
* @since 3.0 * @since 3.0
*/ */
public class HttpHeaders implements MultiValueMap<String, String>, Serializable { public class HttpHeaders implements MultiValueMap<String, String>, Serializable {
...@@ -437,6 +438,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable ...@@ -437,6 +438,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
} }
/**
* Get the list of header values for the given header name, if any.
* @param headerName the header name
* @return the list of header values, or an empty list
* @since 5.2
*/
public List<String> getOrEmpty(Object headerName) {
List<String> values = get(headerName);
return (values != null ? values : Collections.emptyList());
}
/** /**
* Set the list of acceptable {@linkplain MediaType media types}, * Set the list of acceptable {@linkplain MediaType media types},
* as specified by the {@code Accept} header. * as specified by the {@code Accept} header.
......
...@@ -39,8 +39,10 @@ import org.junit.Test; ...@@ -39,8 +39,10 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyCollectionOf; import static org.hamcrest.Matchers.emptyCollectionOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -61,6 +63,21 @@ public class HttpHeadersTests { ...@@ -61,6 +63,21 @@ public class HttpHeadersTests {
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
@Test
public void getOrEmpty() {
String key = "FOO";
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
headers.add(key, "bar");
assertThat(headers.getOrEmpty(key), is(Arrays.asList("bar")));
headers.remove(key);
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
}
@Test @Test
public void getFirst() { public void getFirst() {
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public"); headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册