Update contribution

Closes gh-30300
This commit is contained in:
rstoyanchev
2024-01-04 14:53:13 +00:00
parent a3532bfccc
commit e0d6b69195
4 changed files with 51 additions and 16 deletions

View File

@@ -40,7 +40,6 @@ import org.springframework.lang.Nullable;
* {@code @RestController} or {@code RestControllerAdvice} class.
*
* @author Rossen Stoyanchev
* @author Yanming Zhou
* @since 6.0
* @see ErrorResponseException
*/
@@ -143,14 +142,6 @@ public interface ErrorResponse {
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);

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");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
package org.springframework.web.server;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
@@ -127,6 +130,23 @@ public class ResponseStatusException extends ErrorResponseException {
return HttpHeaders.EMPTY;
}
@Override
public ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Locale locale) {
super.updateAndGetBody(messageSource, locale);
// The reason may be a code (consistent with ResponseStatusExceptionResolver)
if (messageSource != null && getReason() != null && getReason().equals(getBody().getDetail())) {
Object[] arguments = getDetailMessageArguments(messageSource, locale);
String resolved = messageSource.getMessage(getReason(), arguments, null, locale);
if (resolved != null) {
getBody().setDetail(resolved);
}
}
return getBody();
}
@Override
public String getMessage() {
return getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");