Improve API for RFC 7807 in functional endpoints
Closes gh-29462
This commit is contained in:
@@ -47,6 +47,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
@@ -106,6 +107,18 @@ public interface ServerResponse {
|
||||
return new DefaultServerResponseBuilder(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@code ServerResponse} from the given {@link ErrorResponse}.
|
||||
* @param response the {@link ErrorResponse} to initialize from
|
||||
* @return the built response
|
||||
* @since 6.0
|
||||
*/
|
||||
static ServerResponse from(ErrorResponse response) {
|
||||
return status(response.getStatusCode())
|
||||
.headers(headers -> headers.putAll(response.getHeaders()))
|
||||
.body(response.getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder with the given HTTP status.
|
||||
* @param status the response status
|
||||
|
||||
@@ -508,10 +508,14 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
||||
Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode,
|
||||
@Nullable Object[] detailMessageArguments, WebRequest request) {
|
||||
|
||||
ErrorResponse errorResponse = ErrorResponse.createFor(
|
||||
ex, status, null, defaultDetail, detailMessageCode, detailMessageArguments);
|
||||
|
||||
return errorResponse.updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
|
||||
ErrorResponse.Builder builder = ErrorResponse.builder(ex, status, defaultDetail);
|
||||
if (detailMessageCode != null) {
|
||||
builder.detailMessageCode(detailMessageCode);
|
||||
}
|
||||
if (detailMessageArguments != null) {
|
||||
builder.detailMessageArguments(detailMessageArguments);
|
||||
}
|
||||
return builder.build().updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user