Skip to content

Commit a5a1ef6

Browse files
committed
Use instance field for ProblemDetail in ErrorResponse's
Closes gh-32644
1 parent 1b60b86 commit a5a1ef6

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Diff for: 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 9457
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

Diff for: 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
}

Diff for: spring-web/src/main/java/org/springframework/web/multipart/MaxUploadSizeExceededException.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -33,10 +33,9 @@
3333
@SuppressWarnings("serial")
3434
public class MaxUploadSizeExceededException extends MultipartException implements ErrorResponse {
3535

36-
private static final ProblemDetail body =
36+
private final ProblemDetail body =
3737
ProblemDetail.forStatusAndDetail(HttpStatus.PAYLOAD_TOO_LARGE, "Maximum upload size exceeded");
3838

39-
4039
private final long maxUploadSize;
4140

4241

@@ -76,7 +75,7 @@ public HttpStatusCode getStatusCode() {
7675

7776
@Override
7877
public ProblemDetail getBody() {
79-
return body;
78+
return this.body;
8079
}
8180

8281
}

0 commit comments

Comments
 (0)