Skip to content

Commit 018cc1c

Browse files
committed
Merge pull request #23256 from lock14
* pr/23256: Polish 'Allow other "timestamp" types in MVC error model' Allow other "timestamp" types in MVC error model Closes gh-23256
2 parents 775f0fa + d8232b3 commit 018cc1c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.autoconfigure.web.servlet.error;
1818

1919
import java.nio.charset.StandardCharsets;
20-
import java.util.Date;
2120
import java.util.Map;
2221
import java.util.stream.Collectors;
2322

@@ -206,7 +205,7 @@ public void render(Map<String, ?> model, HttpServletRequest request, HttpServlet
206205
}
207206
response.setContentType(TEXT_HTML_UTF8.toString());
208207
StringBuilder builder = new StringBuilder();
209-
Date timestamp = (Date) model.get("timestamp");
208+
Object timestamp = model.get("timestamp");
210209
Object message = model.get("message");
211210
Object trace = model.get("trace");
212211
if (response.getContentType() == null) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.web.servlet.error;
1818

19+
import java.time.Clock;
20+
import java.util.Map;
21+
1922
import org.junit.jupiter.api.Test;
2023
import org.junit.jupiter.api.extension.ExtendWith;
2124

@@ -62,6 +65,22 @@ void renderContainsViewWithExceptionDetails() throws Exception {
6265
});
6366
}
6467

68+
@Test
69+
void renderCanUseJavaTimeTypeAsTimestamp() throws Exception { // gh-23256
70+
this.contextRunner.run((context) -> {
71+
View errorView = context.getBean("error", View.class);
72+
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
73+
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
74+
false);
75+
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, true);
76+
attributes.put("timestamp", Clock.systemUTC().instant());
77+
errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
78+
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
79+
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
80+
assertThat(responseString).contains("This application has no explicit mapping for /error");
81+
});
82+
}
83+
6584
@Test
6685
void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
6786
this.contextRunner.run((context) -> {

0 commit comments

Comments
 (0)