diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 5e51124a54..ad81e95ff8 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1148,6 +1148,7 @@ public class HttpHeaders implements MultiValueMap, Serializable /** * Set the time the resource was last changed, as specified by the * {@code Last-Modified} header. + * @since 5.1.4 */ public void setLastModified(Instant lastModified) { setInstant(LAST_MODIFIED, lastModified); @@ -1156,9 +1157,10 @@ public class HttpHeaders implements MultiValueMap, Serializable /** * Set the time the resource was last changed, as specified by the * {@code Last-Modified} header. + * @since 5.1.4 */ public void setLastModified(ZonedDateTime lastModified) { - setZonedDateTime(LAST_MODIFIED, lastModified); + setZonedDateTime(LAST_MODIFIED, lastModified.withZoneSameInstant(ZoneId.of("GMT"))); } /** @@ -1272,6 +1274,16 @@ public class HttpHeaders implements MultiValueMap, Serializable return getValuesAsList(VARY); } + /** + * Set the given date under the given header name after formatting it as a string + * using the RFC-1123 date-time formatter. The equivalent of + * {@link #set(String, String)} but for date headers. + * @since 5.1.4 + */ + public void setInstant(String headerName, Instant date) { + setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT)); + } + /** * Set the given date under the given header name after formatting it as a string * using the RFC-1123 date-time formatter. The equivalent of @@ -1282,15 +1294,6 @@ public class HttpHeaders implements MultiValueMap, Serializable set(headerName, DATE_FORMATTERS[0].format(date)); } - /** - * Set the given date under the given header name after formatting it as a string - * using the RFC-1123 date-time formatter. The equivalent of - * {@link #set(String, String)} but for date headers. - */ - public void setInstant(String headerName, Instant date) { - setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT)); - } - /** * Set the given date under the given header name after formatting it as a string * using the RFC-1123 date-time formatter. The equivalent of @@ -1299,14 +1302,7 @@ public class HttpHeaders implements MultiValueMap, Serializable * @see #setZonedDateTime(String, ZonedDateTime) */ public void setDate(String headerName, long date) { - set(headerName, formatDate(date)); - } - - // Package private: also used in ResponseCookie.. - static String formatDate(long date) { - Instant instant = Instant.ofEpochMilli(date); - ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT); - return DATE_FORMATTERS[0].format(time); + setInstant(headerName, Instant.ofEpochMilli(date)); } /** @@ -1656,4 +1652,11 @@ public class HttpHeaders implements MultiValueMap, Serializable } } + // Package-private: used in ResponseCookie + static String formatDate(long date) { + Instant instant = Instant.ofEpochMilli(date); + ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT); + return DATE_FORMATTERS[0].format(time); + } + } diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index bbfc4ace30..3d9e0039d9 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -366,18 +366,20 @@ public class ResponseEntity extends HttpEntity { * {@code Last-Modified} header. * @param lastModified the last modified date * @return this builder + * @since 5.1.4 * @see HttpHeaders#setLastModified(long) */ - B lastModified(ZonedDateTime lastModified); + B lastModified(Instant lastModified); /** * Set the time the resource was last changed, as specified by the * {@code Last-Modified} header. * @param lastModified the last modified date * @return this builder + * @since 5.1.4 * @see HttpHeaders#setLastModified(long) */ - B lastModified(Instant lastModified); + B lastModified(ZonedDateTime lastModified); /** * Set the location of a resource, as specified by the {@code Location} header. @@ -516,13 +518,13 @@ public class ResponseEntity extends HttpEntity { } @Override - public BodyBuilder lastModified(ZonedDateTime date) { + public BodyBuilder lastModified(Instant date) { this.headers.setLastModified(date); return this; } @Override - public BodyBuilder lastModified(Instant date) { + public BodyBuilder lastModified(ZonedDateTime date) { this.headers.setLastModified(date); return this; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java index 0da501e6aa..e501daa41c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java @@ -17,9 +17,8 @@ package org.springframework.web.reactive.function.server; import java.net.URI; -import java.time.ZoneId; +import java.time.Instant; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashSet; @@ -158,11 +157,15 @@ class DefaultEntityResponseBuilder implements EntityResponse.Builder { return this; } + @Override + public EntityResponse.Builder lastModified(Instant lastModified) { + this.headers.setLastModified(lastModified); + return this; + } + @Override public EntityResponse.Builder lastModified(ZonedDateTime lastModified) { - ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT")); - String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt); - this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue); + this.headers.setLastModified(lastModified); return this; } @@ -174,10 +177,7 @@ class DefaultEntityResponseBuilder implements EntityResponse.Builder { @Override public EntityResponse.Builder cacheControl(CacheControl cacheControl) { - String ccValue = cacheControl.getHeaderValue(); - if (ccValue != null) { - this.headers.setCacheControl(cacheControl.getHeaderValue()); - } + this.headers.setCacheControl(cacheControl); return this; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index 0b455ecaf8..621e1840f1 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -157,9 +157,15 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { return this; } + @Override + public ServerResponse.BodyBuilder lastModified(Instant lastModified) { + this.headers.setLastModified(lastModified); + return this; + } + @Override public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) { - this.headers.setZonedDateTime(HttpHeaders.LAST_MODIFIED, lastModified); + this.headers.setLastModified(lastModified); return this; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java index 15bc6e30c6..89d330b663 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java @@ -17,6 +17,7 @@ package org.springframework.web.reactive.function.server; import java.net.URI; +import java.time.Instant; import java.time.ZonedDateTime; import java.util.Set; import java.util.function.Consumer; @@ -176,11 +177,23 @@ public interface EntityResponse extends ServerResponse { /** * Set the entity tag of the body, as specified by the {@code ETag} header. - * @param eTag the new entity tag + * @param etag the new entity tag * @return this builder * @see HttpHeaders#setETag(String) */ - Builder eTag(String eTag); + Builder eTag(String etag); + + /** + * Set the time the resource was last changed, as specified by the + * {@code Last-Modified} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. + * @param lastModified the last modified date + * @return this builder + * @since 5.1.4 + * @see HttpHeaders#setLastModified(long) + */ + Builder lastModified(Instant lastModified); /** * Set the time the resource was last changed, as specified by the diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java index 21c370b95f..d9e545ec82 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java @@ -17,6 +17,7 @@ package org.springframework.web.reactive.function.server; import java.net.URI; +import java.time.Instant; import java.time.ZonedDateTime; import java.util.Collection; import java.util.List; @@ -275,6 +276,16 @@ public interface ServerResponse { */ B eTag(String eTag); + /** + * Set the time the resource was last changed, as specified by the + * {@code Last-Modified} header. + * @param lastModified the last modified date + * @return this builder + * @since 5.1.4 + * @see HttpHeaders#setLastModified(long) + */ + B lastModified(Instant lastModified); + /** * Set the time the resource was last changed, as specified by the * {@code Last-Modified} header.