Lazily initialize ProblemDetail for picking up actual status code

Closes gh-35829

(cherry picked from commit 3026f0a49b)
This commit is contained in:
Juergen Hoeller
2025-11-19 17:31:27 +01:00
parent e5de8b9bc6
commit 59025c5b96
3 changed files with 22 additions and 33 deletions
@@ -25,8 +25,8 @@ import org.springframework.lang.Nullable;
import org.springframework.web.ErrorResponse;
/**
* Fatal binding exception, thrown when we want to
* treat binding exceptions as unrecoverable.
* Fatal binding exception, thrown when we want to treat binding exceptions
* as unrecoverable.
*
* <p>Extends ServletException for convenient throwing in any Servlet resource
* (such as a Filter), and NestedServletException for proper root cause handling
@@ -38,13 +38,14 @@ import org.springframework.web.ErrorResponse;
@SuppressWarnings("serial")
public class ServletRequestBindingException extends ServletException implements ErrorResponse {
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
private final String messageDetailCode;
@Nullable
private final Object[] messageDetailArguments;
@Nullable
private ProblemDetail body;
/**
* Constructor with a message only.
@@ -108,7 +109,10 @@ public class ServletRequestBindingException extends ServletException implements
}
@Override
public ProblemDetail getBody() {
public synchronized ProblemDetail getBody() {
if (this.body == null) {
this.body = ProblemDetail.forStatus(getStatusCode());
}
return this.body;
}