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
This commit is contained in:
Sam Brannen
2019-05-22 15:26:45 +02:00
parent 9ca7354146
commit f806594e60
2 changed files with 29 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ import org.springframework.util.StringUtils;
* @author Brian Clozel
* @author Juergen Hoeller
* @author Josh Long
* @author Sam Brannen
* @since 3.0
*/
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},
* as specified by the {@code Accept} header.