From 3cd853f56ca25ca1799cb65dc2439762703f1513 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 6 Apr 2023 17:18:27 +0800 Subject: [PATCH] ResponseStatusException reason as problem detail should be updated via MessageSource Closes GH-30222 --- .../springframework/web/ErrorResponse.java | 9 +++++++ .../ResponseEntityExceptionHandlerTests.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) 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 c83bcdb1c4b8..dfc545bb2daa 100644 --- a/spring-web/src/main/java/org/springframework/web/ErrorResponse.java +++ b/spring-web/src/main/java/org/springframework/web/ErrorResponse.java @@ -40,6 +40,7 @@ * {@code @RestController} or {@code RestControllerAdvice} class. * * @author Rossen Stoyanchev + * @author Yanming Zhou * @since 6.0 * @see ErrorResponseException */ @@ -142,6 +143,14 @@ default ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Lo if (detail != null) { getBody().setDetail(detail); } + else { + // detail from ResponseStatusException reason may be message code + detail = getBody().getDetail(); + if (detail != null) { + detail = messageSource.getMessage(detail, null, detail, locale); + getBody().setDetail(detail); + } + } String title = messageSource.getMessage(getTitleMessageCode(), null, null, locale); if (title != null) { getBody().setTitle(title); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java index b3967ebc18d9..bee3c569e407 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java @@ -63,6 +63,7 @@ import org.springframework.web.method.annotation.HandlerMethodValidationException; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.support.MissingServletRequestPartException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.NoHandlerFoundException; @@ -80,6 +81,7 @@ * * @author Rossen Stoyanchev * @author Sebastien Deleuze + * @author Yanming Zhou */ public class ResponseEntityExceptionHandlerTests { @@ -199,6 +201,30 @@ public void errorResponseProblemDetailViaMessageSource() { } } + @Test + public void reasonAsDetailShouldBeUpdatedViaMessageSource() { + + Locale locale = Locale.UK; + LocaleContextHolder.setLocale(locale); + + String code = "bad.request"; + String message = "Breaking Bad Request"; + try { + StaticMessageSource messageSource = new StaticMessageSource(); + messageSource.addMessage(code, locale, message); + + this.exceptionHandler.setMessageSource(messageSource); + + ResponseEntity entity = testException(new ResponseStatusException(HttpStatus.BAD_REQUEST, code)); + + ProblemDetail body = (ProblemDetail) entity.getBody(); + assertThat(body.getDetail()).isEqualTo(message); + } + finally { + LocaleContextHolder.resetLocaleContext(); + } + } + @Test public void conversionNotSupported() { testException(new ConversionNotSupportedException(new Object(), Object.class, null));