Exceptions
@Controller
和 @ControllerAdvice 类可以有 @ExceptionHandler
方法来处理来自控制器方法的异常。下面的示例包含这样一个处理程序方法:
@Controller
and @ControllerAdvice classes can have
@ExceptionHandler
methods to handle exceptions from controller methods. The following
example includes such a handler method:
include-code::./SimpleController[indent=0]
异常可以与正在传播的顶级异常(即直接抛出的 IOException
)匹配,或与顶级包装异常(例如,包含在 IllegalStateException
中的 IOException
)内的直接原因匹配。
The exception can match against a top-level exception being propagated (that is, a direct
IOException
being thrown) or against the immediate cause within a top-level wrapper
exception (for example, an IOException
wrapped inside an IllegalStateException
).
对于匹配异常类型,最好将目标异常声明为方法参数,如前一示例中所示。或者,该注解声明可缩小异常类型以匹配。我们通常建议在参数签名中尽可能具体并按顺序声明您的主要根异常映射。有关详细信息,请参见 @ControllerAdvice
。
For matching exception types, preferably declare the target exception as a method argument,
as shown in the preceding example. Alternatively, the annotation declaration can narrow the
exception types to match. We generally recommend being as specific as possible in the
argument signature and to declare your primary root exception mappings on a
@ControllerAdvice
prioritized with a corresponding order.
See the MVC section for details.
WebFlux 中的 |
An |
Spring WebFlux 中的 @ExceptionHandler
方法的支持由 @RequestMapping
方法的 HandlerAdapter
提供。有关更多详细信息,请参见 DispatcherHandler
。
Support for @ExceptionHandler
methods in Spring WebFlux is provided by the
HandlerAdapter
for @RequestMapping
methods. See DispatcherHandler
for more detail.
Media Type Mapping
除了异常类型之外,@ExceptionHandler
方法还可以声明可生产的媒体类型。这允许根据 HTTP 客户端请求的媒体类型(通常在“Accept”HTTP 请求头中)来优化错误响应。
In addition to exception types, @ExceptionHandler
methods can also declare producible media types.
This allows to refine error responses depending on the media types requested by HTTP clients, typically in the "Accept" HTTP request header.
应用程序可以直接在注释中为同一异常类型声明可生产的媒体类型:
Applications can declare producible media types directly on annotations, for the same exception type:
/// The provided media type to update the metadata of./// /// /// The operation to be performed./// /// /// The of the fields to update./// /// /// A cancellation token to observe./// /// /// The updated ./// /// /// is null ./// /// /// is null ./// /// /// is not one of the allowed values./// /// /// An error occurred while making the API request./// /// /// An error occurred while making the gRPC request./// [Google.Api.HttpBody(Body = "*" hljs" data-lang="csharp/// /// Updates metadata of a media type./// /// /// The provided media type to update the metadata of./// /// /// The operation to be performed./// /// /// The of the fields to update./// /// /// A cancellation token to observe./// /// /// The updated ./// /// /// is null ./// /// /// is null ./// /// /// is not one of the allowed values./// /// /// An error occurred while making the API request./// /// /// An error occurred while making the gRPC request./// [Google.Api.HttpBody(Body = "*"">[.iokays-original-c6d3a9f857f3eddc1c64ecf5071ad688]
include-code::./MediaTypeController[tag=mediatype,indent=0]
[.iokays-translated-2c8c5ea6035234bd42c2fb63c640f06b]
在此处,方法处理相同的异常类型,但不会因重复而被拒绝。相反,请求“application/json”的 API 客户端将接收一个 JSON 错误,而浏览器会获取一个 HTML 错误视图。每个 `@ExceptionHandler` 注释可以声明多种可生产的媒体类型,错误处理阶段的内容协商将决定使用哪种内容类型。
[.iokays-original-2c8c5ea6035234bd42c2fb63c640f06b]
Here, methods handle the same exception type but will not be rejected as duplicates.
Instead, API clients requesting "application/json" will receive a JSON error, and browsers will get an HTML error view.
Each `@ExceptionHandler` annotation can declare several producible media types,
the content negotiation during the error handling phase will decide which content type will be used.
[id="webflux-ann-exceptionhandler-args"]
== Method Arguments
[role="small"][.small]xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc#mvc-ann-exceptionhandler-args[See equivalent in the Servlet stack]
[.iokays-translated-9bc701bc067bf24392ea823b0b34c82d]
`@ExceptionHandler` 方法支持与 `@RequestMapping` 方法相同的 xref:web/webflux/controller/ann-methods/arguments.adoc[method arguments],但是请求正文可能已用完。
[.iokays-original-9bc701bc067bf24392ea823b0b34c82d]
`@ExceptionHandler` methods support the same xref:web/webflux/controller/ann-methods/arguments.adoc[method arguments]
as `@RequestMapping` methods, except the request body might have been consumed already.
[id="webflux-ann-exceptionhandler-return-values"]
== Return Values
[role="small"][.small]xref:web/webmvc/mvc-controller/ann-exceptionhandler.adoc#mvc-ann-exceptionhandler-return-values[See equivalent in the Servlet stack]
[.iokays-translated-86347abf239b797205f43da26885c411]
`@ExceptionHandler` 方法支持与 `@RequestMapping` 方法相同的 xref:web/webflux/controller/ann-methods/return-types.adoc[return values]。
[.iokays-original-86347abf239b797205f43da26885c411]
`@ExceptionHandler` methods support the same xref:web/webflux/controller/ann-methods/return-types.adoc[return values]
as `@RequestMapping` methods.