From d1d953819ac9f0c0ece5160b96899030cabda46c Mon Sep 17 00:00:00 2001 From: lock14 Date: Fri, 11 Sep 2020 13:24:06 -0700 Subject: [PATCH 1/2] Allow other "timestamp" types in MVC error model Remove casting "timestamp" to `java.util.Date` in `ErrorMvcAutoConfiguration` as the cast is not necessary and it prevents other types (e.g. `java.time`) from being used. See gh-23256 --- .../web/servlet/error/ErrorMvcAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java index 458fc3de8d9..b214796aba8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java @@ -206,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) { From d8232b3c21836e77655781a55d29baffa08bc8db Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 11 Sep 2020 18:43:12 -0700 Subject: [PATCH 2/2] Polish 'Allow other "timestamp" types in MVC error model' See gh-23256 --- .../error/ErrorMvcAutoConfiguration.java | 3 +-- .../error/ErrorMvcAutoConfigurationTests.java | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java index b214796aba8..952a51b6796 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java index 771ced71632..fcb3a16c8e7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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; @@ -62,6 +65,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 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) -> {