Merge branch '2.2.x' into 2.3.x

Closes gh-23258
This commit is contained in:
Phillip Webb 2020-09-11 18:45:05 -07:00
commit 507fae5141
2 changed files with 20 additions and 2 deletions

View File

@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.web.servlet.error;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;
@ -207,7 +206,7 @@ public class ErrorMvcAutoConfiguration {
}
response.setContentType(TEXT_HTML_UTF8.toString());
StringBuilder builder = new StringBuilder();
Date timestamp = (Date) model.get("timestamp");
Object timestamp = model.get("timestamp");
Object message = model.get("message");
Object trace = model.get("trace");
if (response.getContentType() == null) {

View File

@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.web.servlet.error;
import java.time.Clock;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -65,6 +68,22 @@ class ErrorMvcAutoConfigurationTests {
});
}
@Test
void renderCanUseJavaTimeTypeAsTimestamp() throws Exception { // gh-23256
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
false);
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, true);
attributes.put("timestamp", Clock.systemUTC().instant());
errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
assertThat(responseString).contains("This application has no explicit mapping for /error");
});
}
@Test
void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
this.contextRunner.run((context) -> {