From 5288504ceb17652945e6a77ed5606474466807a0 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 13 May 2024 11:14:02 +0100 Subject: [PATCH] Use instance field for ProblemDetail in ErrorResponse's Closes gh-32644 --- .../main/java/org/springframework/web/ErrorResponse.java | 5 +++++ .../request/async/AsyncRequestTimeoutException.java | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java index 2ae0187d70..3bc3cb2679 100644 --- a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java +++ b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java @@ -61,6 +61,11 @@ public interface ErrorResponse { * Return the body for the response, formatted as an RFC 7807 * {@link ProblemDetail} whose {@link ProblemDetail#getStatus() status} * should match the response status. + *

Note: The returned {@code ProblemDetail} may be + * updated before the response is rendered, e.g. via + * {@link #updateAndGetBody(MessageSource, Locale)}. Therefore, implementing + * methods should use an instance field, and should not re-create the + * {@code ProblemDetail} on every call, nor use a static variable. */ ProblemDetail getBody(); diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/AsyncRequestTimeoutException.java b/spring-web/src/main/java/org/springframework/web/context/request/async/AsyncRequestTimeoutException.java index 100dd59303..ea0e57f608 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/AsyncRequestTimeoutException.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/AsyncRequestTimeoutException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -37,6 +37,9 @@ import org.springframework.web.ErrorResponse; @SuppressWarnings("serial") public class AsyncRequestTimeoutException extends RuntimeException implements ErrorResponse { + private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode()); + + @Override public HttpStatusCode getStatusCode() { return HttpStatus.SERVICE_UNAVAILABLE; @@ -44,7 +47,7 @@ public class AsyncRequestTimeoutException extends RuntimeException implements Er @Override public ProblemDetail getBody() { - return ProblemDetail.forStatus(getStatusCode()); + return this.body; } }