@@ -3496,19 +3496,46 @@ include::webflux-cors.adoc[leveloffset=+1]
3496
3496
[.small]#<<web.adoc#mvc-ann-rest-exceptions, Web MVC>>#
3497
3497
3498
3498
A common requirement for REST services is to include error details in the body of the
3499
- response. The Spring Framework does not automatically do so, because the representation
3500
- of error details in the response body is application-specific. However, a
3501
- `@RestController` can use `@ExceptionHandler` methods with a `ResponseEntity` return
3502
- value to set the status and the body of the response. Such methods can also be declared
3503
- in `@ControllerAdvice` classes to apply them globally.
3504
-
3505
- Applications that implement global exception handling with error details in the response
3506
- body should consider extending
3507
- {api-spring-framework}/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.html[`ResponseEntityExceptionHandler`],
3508
- which provides handling for exceptions that Spring MVC raises and provides hooks to
3509
- customize the response body. To make use of this, create a subclass of
3510
- `ResponseEntityExceptionHandler`, annotate it with `@ControllerAdvice`, override the
3511
- necessary methods, and declare it as a Spring bean.
3499
+ response. The Spring Framework provides support for
3500
+ https://www.rfc-editor.org/rfc/rfc7807.html[RFC 7807] formatted error responses.
3501
+
3502
+ Main abstractions and supporting infrastructure:
3503
+
3504
+ - `ProblemDetail` in the `spring-web` module is the core abstraction that represents an
3505
+ RFC 7807 problem detail. It helps to enable a range of features in Spring WebFlux for the
3506
+ handling and rendering of such responses.
3507
+ - `ErrorResponse` is an interface that defines an error response, including status, headers,
3508
+ and `ProblemDetail` as its body. All Spring web exceptions implement this interface, and
3509
+ thus encapsulate a default opinion on how they map to an RFC 7807 error response.
3510
+ - `ErrorResponseException` is a `RuntimeException` that implements `ErrorResponse`, which
3511
+ can be raised directly or serve as a base class for other exceptions.
3512
+ - {api-spring-framework}/web/reactive/result/method/annotation/ResponseEntityExceptionHandler.html[`ResponseEntityExceptionHandler`]
3513
+ provides handling for Spring WebFlux exceptions and for any `ErrorResponseException`.
3514
+ Applications can extend this as an <<webflux-ann-controller-advice>> to enable RFC 7807 support.
3515
+
3516
+ `ProblemDetail` and `ErrorResponse` are supported as return values from
3517
+ `@ExceptionHandler` and `@RequestMapping` controller methods. The `status` property of
3518
+ `ProblemDetail` is used to set the response status, while the `instance` property is set
3519
+ from the current URL path, if not already set.
3520
+
3521
+ The Jackson `Encoder` returns "application/problem+json" as a preferred
3522
+ choice to serialize `ProblemDetail` to JSON as part of content negotiation. If no suitable
3523
+ media type is found to render a `ProblemDetail` response body, content negotiation falls
3524
+ back on "application/problem+json".
3525
+
3526
+ Applications can extend `ProblemDetail` with non-standard fields in one of two ways:
3527
+
3528
+ . Add properties to the generic `properties` map in `ProblemDetail`. When using
3529
+ the Jackson library, this `properties` map is unwrapped and as top level JSON
3530
+ properties with the help of `ProblemDetailJacksonMixin`.
3531
+ . Create a `ProblemDetail` subclass that defines the extra, non-standard fields.
3532
+ Subclasses can use a protected copy constructor in order to re-create an existing
3533
+ `ProblemDetail` as a subclass. This can be done centrally from an `@ControllerAdvice`
3534
+ such as `ResponseEntityExceptionHandler`.
3535
+
3536
+ On the client side, `WebClientResponseException` provides methods that decode the response
3537
+ body to some target type. This is useful to decode to a `ProblemDetail`, or to any other
3538
+ class, including subclasses of `ProblemDetail`.
3512
3539
3513
3540
3514
3541
0 commit comments