Use instance field for ProblemDetail in ErrorResponse's

Closes gh-32644
This commit is contained in:
rstoyanchev 2024-05-13 11:14:02 +01:00
parent 1b60b86bb2
commit a5a1ef6b30
3 changed files with 13 additions and 6 deletions

View File

@ -61,6 +61,11 @@ public interface ErrorResponse {
* Return the body for the response, formatted as an RFC 9457 * Return the body for the response, formatted as an RFC 9457
* {@link ProblemDetail} whose {@link ProblemDetail#getStatus() status} * {@link ProblemDetail} whose {@link ProblemDetail#getStatus() status}
* should match the response status. * should match the response status.
* <p><strong>Note:</strong> 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(); ProblemDetail getBody();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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") @SuppressWarnings("serial")
public class AsyncRequestTimeoutException extends RuntimeException implements ErrorResponse { public class AsyncRequestTimeoutException extends RuntimeException implements ErrorResponse {
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
@Override @Override
public HttpStatusCode getStatusCode() { public HttpStatusCode getStatusCode() {
return HttpStatus.SERVICE_UNAVAILABLE; return HttpStatus.SERVICE_UNAVAILABLE;
@ -44,7 +47,7 @@ public class AsyncRequestTimeoutException extends RuntimeException implements Er
@Override @Override
public ProblemDetail getBody() { public ProblemDetail getBody() {
return ProblemDetail.forStatus(getStatusCode()); return this.body;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,10 +33,9 @@ import org.springframework.web.ErrorResponse;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class MaxUploadSizeExceededException extends MultipartException implements ErrorResponse { public class MaxUploadSizeExceededException extends MultipartException implements ErrorResponse {
private static final ProblemDetail body = private final ProblemDetail body =
ProblemDetail.forStatusAndDetail(HttpStatus.PAYLOAD_TOO_LARGE, "Maximum upload size exceeded"); ProblemDetail.forStatusAndDetail(HttpStatus.PAYLOAD_TOO_LARGE, "Maximum upload size exceeded");
private final long maxUploadSize; private final long maxUploadSize;
@ -76,7 +75,7 @@ public class MaxUploadSizeExceededException extends MultipartException implement
@Override @Override
public ProblemDetail getBody() { public ProblemDetail getBody() {
return body; return this.body;
} }
} }