Add ErrorResponse and ErrorResponseException
ErrorResponse represents a complete error response with status, headers, and an RFC 7807 ProblemDetail body. ErrorResponseException implements ErrorResponse and is usable on its own or as a base class. ResponseStatusException extends ErrorResponseException and now also supports RFC 7807 and so does its sub-hierarchy. ErrorResponse can be returned from `@ExceptionHandler` methods and is mapped to ResponseEntity. See gh-27052
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -116,7 +116,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
/**
|
||||
* Template method that handles an {@link ResponseStatusException}.
|
||||
* <p>The default implementation applies the headers from
|
||||
* {@link ResponseStatusException#getResponseHeaders()} and delegates to
|
||||
* {@link ResponseStatusException#getHeaders()} and delegates to
|
||||
* {@link #applyStatusAndReason} with the status code and reason from the
|
||||
* exception.
|
||||
* @param ex the exception
|
||||
@@ -130,9 +130,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
|
||||
protected ModelAndView resolveResponseStatusException(ResponseStatusException ex,
|
||||
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception {
|
||||
|
||||
ex.getResponseHeaders().forEach((name, values) ->
|
||||
values.forEach(value -> response.addHeader(name, value)));
|
||||
|
||||
ex.getHeaders().forEach((name, values) -> values.forEach(value -> response.addHeader(name, value)));
|
||||
return applyStatusAndReason(ex.getRawStatusCode(), ex.getReason(), response);
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.ErrorResponse;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
@@ -56,7 +57,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
/**
|
||||
* Resolves {@link HttpEntity} and {@link RequestEntity} method argument values,
|
||||
* as well as return values of type {@link HttpEntity}, {@link ResponseEntity},
|
||||
* and {@link ProblemDetail}.
|
||||
* {@link ErrorResponse} and {@link ProblemDetail}.
|
||||
*
|
||||
* <p>An {@link HttpEntity} return type has a specific purpose. Therefore, this
|
||||
* handler should be configured ahead of handlers that support any return
|
||||
@@ -122,7 +123,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
||||
public boolean supportsReturnType(MethodParameter returnType) {
|
||||
Class<?> type = returnType.getParameterType();
|
||||
return ((HttpEntity.class.isAssignableFrom(type) && !RequestEntity.class.isAssignableFrom(type)) ||
|
||||
ProblemDetail.class.isAssignableFrom(type));
|
||||
ErrorResponse.class.isAssignableFrom(type) || ProblemDetail.class.isAssignableFrom(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -180,7 +181,10 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
|
||||
ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
|
||||
|
||||
HttpEntity<?> httpEntity;
|
||||
if (returnValue instanceof ProblemDetail detail) {
|
||||
if (returnValue instanceof ErrorResponse response) {
|
||||
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getRawStatusCode());
|
||||
}
|
||||
else if (returnValue instanceof ProblemDetail detail) {
|
||||
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user