Skip to content

Commit deca37d

Browse files
committed
Improve Javadoc for new HttpHeaders features
1 parent 347040c commit deca37d

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,15 @@ public class HttpHeaders implements Serializable {
439439

440440

441441
/**
442-
* Construct a new, empty instance of the {@code HttpHeaders} object
443-
* using an underlying case-insensitive map.
442+
* Construct a new, empty {@code HttpHeaders} instance using an underlying
443+
* case-insensitive map.
444444
*/
445445
public HttpHeaders() {
446446
this(CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ROOT)));
447447
}
448448

449449
/**
450-
* Construct a new {@code HttpHeaders} instance backed by an existing map.
450+
* Construct a new {@code HttpHeaders} instance backed by the supplied map.
451451
* <p>This constructor is available as an optimization for adapting to existing
452452
* headers map structures, primarily for internal use within the framework.
453453
* @param headers the headers map (expected to operate with case-insensitive keys)
@@ -459,13 +459,21 @@ public HttpHeaders(MultiValueMap<String, String> headers) {
459459
}
460460

461461
/**
462-
* Construct a new {@code HttpHeaders} instance by removing any read-only
463-
* wrapper that may have been previously applied around the given
464-
* {@code HttpHeaders} via {@link #readOnlyHttpHeaders(HttpHeaders)}.
465-
* <p>Once the writable instance is mutated, the read-only instance is
462+
* Construct a new {@code HttpHeaders} instance backed by the supplied
463+
* {@code HttpHeaders}.
464+
* <p>Changes to the {@code HttpHeaders} created by this constructor will
465+
* write through to the supplied {@code HttpHeaders}. If you wish to copy
466+
* an existing {@code HttpHeaders} instance, use {@link #copyOf(HttpHeaders)}
467+
* instead.
468+
* <p>If the supplied {@code HttpHeaders} instance is a
469+
* {@linkplain #readOnlyHttpHeaders(HttpHeaders) read-only}
470+
* {@code HttpHeaders} wrapper, it will be unwrapped to ensure that the
471+
* {@code HttpHeaders} instance created by this constructor is mutable.
472+
* Once the writable instance is mutated, the read-only instance is
466473
* likely to be out of sync and should be discarded.
467474
* @param httpHeaders the headers to expose
468475
* @since 7.0
476+
* @see #copyOf(HttpHeaders)
469477
*/
470478
public HttpHeaders(HttpHeaders httpHeaders) {
471479
Assert.notNull(httpHeaders, "HttpHeaders must not be null");
@@ -481,7 +489,10 @@ public HttpHeaders(HttpHeaders httpHeaders) {
481489
}
482490

483491
/**
484-
* Create a new {@code HttpHeaders} mutable instance and copy all header values given as a parameter.
492+
* Create a new, mutable {@code HttpHeaders} instance and copy the supplied
493+
* headers to that new instance.
494+
* <p>Changes to the returned {@code HttpHeaders} will not affect the
495+
* supplied headers map.
485496
* @param headers the headers to copy
486497
* @since 7.0
487498
*/
@@ -492,9 +503,13 @@ public static HttpHeaders copyOf(MultiValueMap<String, String> headers) {
492503
}
493504

494505
/**
495-
* Create a new {@code HttpHeaders} mutable instance and copy all header values given as a parameter.
506+
* Create a new, mutable {@code HttpHeaders} instance and copy the supplied
507+
* headers to that new instance.
508+
* <p>Changes to the returned {@code HttpHeaders} will not affect the
509+
* supplied {@code HttpHeaders}.
496510
* @param httpHeaders the headers to copy
497511
* @since 7.0
512+
* @see #HttpHeaders(HttpHeaders)
498513
*/
499514
public static HttpHeaders copyOf(HttpHeaders httpHeaders) {
500515
return copyOf(httpHeaders.headers);

spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ void constructorUnwrapsReadonly() {
6767
// content-type value is cached by ReadOnlyHttpHeaders
6868
assertThat(readOnly.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
6969
assertThat(writable.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
70+
// The HttpHeaders "copy constructor" creates an HttpHeaders instance
71+
// that mutates the state of the original HttpHeaders instance.
72+
assertThat(headers.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
7073
}
7174

7275
@Test

0 commit comments

Comments
 (0)