Improve API for RFC 7807 in functional endpoints
Closes gh-29462
This commit is contained in:
@@ -42,6 +42,7 @@ import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.json.Jackson2CodecSupport;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.reactive.function.BodyInserter;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
@@ -104,6 +105,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 {@code Mono} with the built response
|
||||
* @since 6.0
|
||||
*/
|
||||
static Mono<ServerResponse> from(ErrorResponse response) {
|
||||
return status(response.getStatusCode())
|
||||
.headers(headers -> headers.putAll(response.getHeaders()))
|
||||
.bodyValue(response.getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a builder with the given HTTP status.
|
||||
* @param status the response status
|
||||
|
||||
@@ -309,10 +309,14 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
||||
Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode,
|
||||
@Nullable Object[] detailMessageArguments, ServerWebExchange exchange) {
|
||||
|
||||
ErrorResponse response = ErrorResponse.createFor(
|
||||
ex, status, null, defaultDetail, detailMessageCode, detailMessageArguments);
|
||||
|
||||
return response.updateAndGetBody(this.messageSource, getLocale(exchange));
|
||||
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, getLocale(exchange));
|
||||
}
|
||||
|
||||
private static Locale getLocale(ServerWebExchange exchange) {
|
||||
|
||||
Reference in New Issue
Block a user