public interface HttpHeaders extends HttpObject
HttpHeadersYou can use the HttpHeaders.of() factory methods or
the HttpHeadersBuilder to build a new HttpHeaders from scratch:
// Using of()
HttpHeaders headersWithOf =
HttpHeaders.of(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8",
HttpHeaderNames.CONTENT_LENGTH, "42");
// Using builder()
HttpHeaders headersWithBuilder =
HttpHeaders.builder()
.add(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8")
.add(HttpHeaderNames.CONTENT_LENGTH, "42")
.build();
assert headersWithOf.equals(headersWithBuilder);
HttpHeaders from an existing oneYou can use toBuilder() or withMutations(Consumer) to build
a new HttpHeaders derived from an existing one:
HttpHeaders headers = HttpHeaders.of("name1", "value0");
// Using toBuilder()
HttpHeaders headersWithToBuilder = headers.toBuilder()
.set("name1", "value1")
.add("name2", "value2")
.build();
// Using withMutations()
HttpHeaders headersWithMutations = headers.withMutations(builder -> {
builder.set("name1", "value1");
builder.add("name2", "value2");
});
assert headersWithToBuilder.equals(headersWithMutations);
// Note that the original headers remain unmodified.
assert !headers.equals(headersWithToBuilder);
assert !headers.equals(headersWithMutations);
String header valueCertain header values are better represented as a Java object than as a String.
For example, it is more convenient to specify "content-length", "content-type" and
"date" header as Integer, MediaType and Instant (or Date)
respectively. Armeria's HTTP header API allows you to specify a Java object of well-known type
as a header value by converting it into an HTTP-friendly String representation:
Number, CharSequence and MediaType
toString()"42", "string", "text/plain; charset=utf-8"CacheControl
asHeaderValue()"no-cache, no-store, must-revalidate"Instant, TemporalAccessor, Date and Calendar
Sun, 27 Nov 2016 19:37:15 UTCtoString()HttpHeaders.of() factory methods
HttpHeaders headers =
HttpHeaders.of(HttpHeaderNames.CONTENT_LENGTH, 42,
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8,
HttpHeaderNames.DATE, Instant.now());
HttpHeadersBuilder
HttpHeaders headers =
HttpHeaders.builder()
.setObject(HttpHeaderNames.CONTENT_LENGTH, 42)
.setObject(HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8)
.setObject(HttpHeaderNames.DATE, Instant.now())
.build();
You might prefer type-safe setters for more efficiency and less ambiguity:
HttpHeaders headers =
HttpHeaders.builder()
.setInt(HttpHeaderNames.CONTENT_LENGTH, 42)
.set(HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8.toString())
.setTimeMillis(HttpHeaderNames.DATE, System.currentTimeMillis())
.build();
RequestHeaders,
ResponseHeaders| Modifier and Type | Field and Description |
|---|---|
static HttpHeaders |
EMPTY_HEADERS
Deprecated.
Use
of(). |
| Modifier and Type | Method and Description |
|---|---|
static HttpHeadersBuilder |
builder()
Returns a new empty builder.
|
boolean |
contains(CharSequence name)
Returns
true if a header with the name exists, false otherwise. |
boolean |
contains(CharSequence name,
String value)
Returns
true if a header with the name and value exists. |
boolean |
containsDouble(CharSequence name,
double value)
Returns
true if a header with the name and value exists. |
boolean |
containsFloat(CharSequence name,
float value)
Returns
true if a header with the name and value exists. |
boolean |
containsInt(CharSequence name,
int value)
Returns
true if a header with the name and value exists. |
boolean |
containsLong(CharSequence name,
long value)
Returns
true if a header with the name and value exists. |
boolean |
containsObject(CharSequence name,
Object value)
Returns
true if a header with the name and value exists. |
boolean |
containsTimeMillis(CharSequence name,
long value)
Returns
true if a header with the name and value exists. |
MediaType |
contentType()
Returns the parsed
"content-type" header. |
void |
forEach(BiConsumer<io.netty.util.AsciiString,String> action)
Invokes the specified
action for all header entries. |
void |
forEachValue(CharSequence name,
Consumer<String> action)
Invokes the specified
action for all values of the headers with the specified name. |
String |
get(CharSequence name)
Returns the value of a header with the specified
name. |
String |
get(CharSequence name,
String defaultValue)
Returns the value of a header with the specified
name. |
List<String> |
getAll(CharSequence name)
Returns all values for the header with the specified name.
|
Double |
getDouble(CharSequence name)
Returns the
double value of a header with the specified name. |
double |
getDouble(CharSequence name,
double defaultValue)
Returns the
double value of a header with the specified name. |
Float |
getFloat(CharSequence name)
Returns the
float value of a header with the specified name. |
float |
getFloat(CharSequence name,
float defaultValue)
Returns the
float value of a header with the specified name. |
Integer |
getInt(CharSequence name)
Returns the
int value of a header with the specified name. |
int |
getInt(CharSequence name,
int defaultValue)
Returns the
int value of a header with the specified name. |
Long |
getLong(CharSequence name)
Returns the
long value of a header with the specified name. |
long |
getLong(CharSequence name,
long defaultValue)
Returns the
long value of a header with the specified name. |
Long |
getTimeMillis(CharSequence name)
Returns the value of a header with the specified
name in milliseconds. |
long |
getTimeMillis(CharSequence name,
long defaultValue)
Returns the value of a header with the specified
name in milliseconds. |
boolean |
isEmpty()
Returns
true if this headers does not contain any entries. |
boolean |
isEndOfStream()
Tells whether the headers correspond to the last frame in an HTTP/2 stream.
|
Iterator<Map.Entry<io.netty.util.AsciiString,String>> |
iterator()
Returns an
Iterator that yields all header entries. |
Set<io.netty.util.AsciiString> |
names()
Returns a
Set of all header names. |
static HttpHeaders |
of()
Returns an empty
HttpHeaders. |
static HttpHeaders |
of(CharSequence name,
Object value)
Returns a new
HttpHeaders with the specified header. |
static HttpHeaders |
of(CharSequence name1,
Object value1,
CharSequence name2,
Object value2)
Returns a new
HttpHeaders with the specified headers. |
static HttpHeaders |
of(CharSequence name1,
Object value1,
CharSequence name2,
Object value2,
CharSequence name3,
Object value3)
Returns a new
HttpHeaders with the specified headers. |
static HttpHeaders |
of(CharSequence name1,
Object value1,
CharSequence name2,
Object value2,
CharSequence name3,
Object value3,
CharSequence name4,
Object value4)
Returns a new
HttpHeaders with the specified headers. |
static HttpHeaders |
of(CharSequence name,
String value)
Returns a new
HttpHeaders with the specified header. |
static HttpHeaders |
of(CharSequence name1,
String value1,
CharSequence name2,
String value2)
Returns a new
HttpHeaders with the specified headers. |
static HttpHeaders |
of(CharSequence name1,
String value1,
CharSequence name2,
String value2,
CharSequence name3,
String value3)
Returns a new
HttpHeaders with the specified headers. |
static HttpHeaders |
of(CharSequence name1,
String value1,
CharSequence name2,
String value2,
CharSequence name3,
String value3,
CharSequence name4,
String value4)
Returns a new
HttpHeaders with the specified headers. |
int |
size()
Returns the number of headers.
|
default Stream<Map.Entry<io.netty.util.AsciiString,String>> |
stream()
Returns a
Stream that yields all header entries. |
HttpHeadersBuilder |
toBuilder()
Returns a new builder created from the entries of this headers.
|
Iterator<String> |
valueIterator(CharSequence name)
Returns an
Iterator that yields all values of the headers with the specified name. |
default Stream<String> |
valueStream(CharSequence name)
Returns a
Stream that yields all values of the headers with the specified name. |
default HttpHeaders |
withMutations(Consumer<HttpHeadersBuilder> mutator)
Returns a new headers which is the result from the mutation by the specified
Consumer. |
isEndOfStreamforEach, spliterator@Deprecated static final HttpHeaders EMPTY_HEADERS
of().HttpHeaders.static HttpHeadersBuilder builder()
static HttpHeaders of()
HttpHeaders.static HttpHeaders of(CharSequence name, String value)
HttpHeaders with the specified header.static HttpHeaders of(CharSequence name, Object value)
HttpHeaders with the specified header. The value is converted into
a String as explained in Specifying a non-String header value.static HttpHeaders of(CharSequence name1, String value1, CharSequence name2, String value2)
HttpHeaders with the specified headers.static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2)
HttpHeaders with the specified headers. The values are converted into
Strings as explained in Specifying a non-String header value.static HttpHeaders of(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3)
HttpHeaders with the specified headers.static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3)
HttpHeaders with the specified headers. The values are converted into
Strings as explained in Specifying a non-String header value.static HttpHeaders of(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3, CharSequence name4, String value4)
HttpHeaders with the specified headers.static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3, CharSequence name4, Object value4)
HttpHeaders with the specified headers. The values are converted into
Strings as explained in Specifying a non-String header value.HttpHeadersBuilder toBuilder()
withMutations(Consumer)default HttpHeaders withMutations(Consumer<HttpHeadersBuilder> mutator)
Consumer.
This method is a shortcut for:
builder = toBuilder();
mutator.accept(builder);
return builder.build();
toBuilder()boolean isEndOfStream()
MediaType contentType()
"content-type" header.MediaType if present and valid, or null otherwise.String get(CharSequence name)
name. If there are more than one value for
the specified name, the first value in insertion order is returned.name - the name of the header to retrievenull if there's no such headerString get(CharSequence name, String defaultValue)
name. If there are more than one value for
the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valuedefaultValue if there is no such headerList<String> getAll(CharSequence name)
List can't be modified.Integer getInt(CharSequence name)
int value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrieveint value of the first value in insertion order or null if there is no such
header or it can't be converted to int.int getInt(CharSequence name, int defaultValue)
int value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valueint value of the first value in insertion order or defaultValue if there is
no such header or it can't be converted to int.Long getLong(CharSequence name)
long value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievelong value of the first value in insertion order or null if there is no such
header or it can't be converted to long.long getLong(CharSequence name, long defaultValue)
long value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valuelong value of the first value in insertion order or defaultValue if there is
no such header or it can't be converted to long.Float getFloat(CharSequence name)
float value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievefloat value of the first value in insertion order or null if there is no
such header or it can't be converted to float.float getFloat(CharSequence name, float defaultValue)
float value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valuefloat value of the first value in insertion order or defaultValue if there
is no such header or it can't be converted to float.Double getDouble(CharSequence name)
double value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedouble value of the first value in insertion order or null if there is no
such header or it can't be converted to double.double getDouble(CharSequence name, double defaultValue)
double value of a header with the specified name. If there are more than one
value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valuedouble value of the first value in insertion order or defaultValue if there
is no such header or it can't be converted to double.Long getTimeMillis(CharSequence name)
name in milliseconds. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievenull if there is no such
header or it can't be converted to milliseconds.long getTimeMillis(CharSequence name, long defaultValue)
name in milliseconds. If there are more than
one value for the specified name, the first value in insertion order is returned.name - the name of the header to retrievedefaultValue - the default valuedefaultValue if there is
no such header or it can't be converted to milliseconds.boolean contains(CharSequence name)
true if a header with the name exists, false otherwise.name - the header nameboolean contains(CharSequence name, String value)
true if a header with the name and value exists.name - the header namevalue - the header value of the header to findboolean containsObject(CharSequence name, Object value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseboolean containsInt(CharSequence name, int value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseboolean containsLong(CharSequence name, long value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseboolean containsFloat(CharSequence name, float value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseboolean containsDouble(CharSequence name, double value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseboolean containsTimeMillis(CharSequence name, long value)
true if a header with the name and value exists.name - the header namevalue - the header valuetrue if the header exists. false otherwiseint size()
boolean isEmpty()
true if this headers does not contain any entries.Set<io.netty.util.AsciiString> names()
Iterator<Map.Entry<io.netty.util.AsciiString,String>> iterator()
Iterator that yields all header entries. The iteration order is as follows:
Iterator<String> valueIterator(CharSequence name)
Iterator that yields all values of the headers with the specified name.void forEach(BiConsumer<io.netty.util.AsciiString,String> action)
action for all header entries.void forEachValue(CharSequence name, Consumer<String> action)
action for all values of the headers with the specified name.default Stream<Map.Entry<io.netty.util.AsciiString,String>> stream()
Stream that yields all header entries.default Stream<String> valueStream(CharSequence name)
Stream that yields all values of the headers with the specified name.Copyright © 2020 LeanCloud. All rights reserved.