Skip to content

Commit 5288504

Browse files
committed
Use instance field for ProblemDetail in ErrorResponse's
Closes gh-32644
1 parent 8b6a54c commit 5288504

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

spring-web/src/main/java/org/springframework/web/ErrorResponse.java

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ default HttpHeaders getHeaders() {
6161
* Return the body for the response, formatted as an RFC 7807
6262
* {@link ProblemDetail} whose {@link ProblemDetail#getStatus() status}
6363
* should match the response status.
64+
* <p><strong>Note:</strong> The returned {@code ProblemDetail} may be
65+
* updated before the response is rendered, e.g. via
66+
* {@link #updateAndGetBody(MessageSource, Locale)}. Therefore, implementing
67+
* methods should use an instance field, and should not re-create the
68+
* {@code ProblemDetail} on every call, nor use a static variable.
6469
*/
6570
ProblemDetail getBody();
6671

spring-web/src/main/java/org/springframework/web/context/request/async/AsyncRequestTimeoutException.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -37,14 +37,17 @@
3737
@SuppressWarnings("serial")
3838
public class AsyncRequestTimeoutException extends RuntimeException implements ErrorResponse {
3939

40+
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
41+
42+
4043
@Override
4144
public HttpStatusCode getStatusCode() {
4245
return HttpStatus.SERVICE_UNAVAILABLE;
4346
}
4447

4548
@Override
4649
public ProblemDetail getBody() {
47-
return ProblemDetail.forStatus(getStatusCode());
50+
return this.body;
4851
}
4952

5053
}

0 commit comments

Comments
 (0)