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:
Brian Clozel
2015-06-17 10:08:58 +02:00
parent 8743b6bb30
commit a421bd2c27
2 changed files with 17 additions and 3 deletions

View File

@@ -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);