Avoid duplicate Etag/Last-Modified header values
This commit improves SPR-13090 and avoids adding duplicate ETag and Last-Modified headers in HTTP responses. Previously, those were added twice to the response since: * we're adding all ResponseEntity headers to the response * the `checkNotModified` methods automatically add those headers Issue: SPR-13090
This commit is contained in:
parent
8743b6bb30
commit
a421bd2c27
|
|
@ -168,12 +168,15 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
|||
}
|
||||
|
||||
HttpHeaders entityHeaders = responseEntity.getHeaders();
|
||||
if (!entityHeaders.isEmpty()) {
|
||||
outputMessage.getHeaders().putAll(entityHeaders);
|
||||
}
|
||||
|
||||
Object body = responseEntity.getBody();
|
||||
if (responseEntity instanceof ResponseEntity) {
|
||||
for (String headerName : entityHeaders.keySet()) {
|
||||
if(!HttpHeaders.LAST_MODIFIED.equals(headerName)
|
||||
&& !HttpHeaders.ETAG.equals(headerName)) {
|
||||
outputMessage.getHeaders().put(headerName, entityHeaders.get(headerName));
|
||||
}
|
||||
}
|
||||
if (isResourceNotModified(webRequest, (ResponseEntity<?>) responseEntity)) {
|
||||
// Ensure headers are flushed, no body should be written.
|
||||
outputMessage.flush();
|
||||
|
|
@ -181,6 +184,11 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
|||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!entityHeaders.isEmpty()) {
|
||||
outputMessage.getHeaders().putAll(entityHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
// Try even with null body. ResponseBodyAdvice could get involved.
|
||||
writeWithMessageConverters(body, returnType, inputMessage, outputMessage);
|
||||
|
|
|
|||
|
|
@ -349,6 +349,7 @@ public class HttpEntityMethodProcessorMockTests {
|
|||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
|
||||
assertEquals(dateFormat.format(oneMinuteAgo), servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
|
||||
assertEquals(0, servletResponse.getContentAsByteArray().length);
|
||||
}
|
||||
|
|
@ -369,6 +370,7 @@ public class HttpEntityMethodProcessorMockTests {
|
|||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.ETAG).size());
|
||||
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
|
||||
assertEquals(0, servletResponse.getContentAsByteArray().length);
|
||||
}
|
||||
|
|
@ -393,7 +395,9 @@ public class HttpEntityMethodProcessorMockTests {
|
|||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
|
||||
assertEquals(dateFormat.format(oneMinuteAgo), servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.ETAG).size());
|
||||
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
|
||||
assertEquals(0, servletResponse.getContentAsByteArray().length);
|
||||
}
|
||||
|
|
@ -419,7 +423,9 @@ public class HttpEntityMethodProcessorMockTests {
|
|||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
assertEquals(HttpStatus.OK.value(), servletResponse.getStatus());
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
|
||||
assertEquals(dateFormat.format(oneMinuteAgo), servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.ETAG).size());
|
||||
assertEquals(changedEtagValue, servletResponse.getHeader(HttpHeaders.ETAG));
|
||||
assertEquals(0, servletResponse.getContentAsByteArray().length);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue