Skip to content

Commit ff9a4ab

Browse files
committed
Update documentation for RFC 7807 support
Closes gh-28438
1 parent eea793b commit ff9a4ab

File tree

2 files changed

+80
-27
lines changed

2 files changed

+80
-27
lines changed

src/docs/asciidoc/web/webflux.adoc

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3496,19 +3496,46 @@ include::webflux-cors.adoc[leveloffset=+1]
34963496
[.small]#<<web.adoc#mvc-ann-rest-exceptions, Web MVC>>#
34973497

34983498
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`.
35123539

35133540

35143541

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,20 +4824,46 @@ include::webmvc-cors.adoc[leveloffset=+1]
48244824
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions, WebFlux>>#
48254825

48264826
A common requirement for REST services is to include error details in the body of the
4827-
response. The Spring Framework does not automatically do this because the representation
4828-
of error details in the response body is application-specific. However, a
4829-
`@RestController` may use `@ExceptionHandler` methods with a `ResponseEntity` return
4830-
value to set the status and the body of the response. Such methods can also be declared
4831-
in `@ControllerAdvice` classes to apply them globally.
4832-
4833-
Applications that implement global exception handling with error details in the response
4834-
body should consider extending
4835-
{api-spring-framework}/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.html[`ResponseEntityExceptionHandler`],
4836-
which provides handling for exceptions that Spring MVC raises and provides hooks to
4837-
customize the response body. To make use of this, create a subclass of
4838-
`ResponseEntityExceptionHandler`, annotate it with `@ControllerAdvice`, override the
4839-
necessary methods, and declare it as a Spring bean.
4840-
4827+
response. The Spring Framework provides support for
4828+
https://www.rfc-editor.org/rfc/rfc7807.html[RFC 7807] formatted error responses.
4829+
4830+
Main abstractions and supporting infrastructure:
4831+
4832+
- `ProblemDetail` in the `spring-web` module is the core abstraction that represents an
4833+
RFC 7807 problem detail. It helps to enable a range of features in Spring MVC for the
4834+
handling and rendering of such responses.
4835+
- `ErrorResponse` is an interface that defines an error response, including status, headers,
4836+
and `ProblemDetail` as its body. All Spring web exceptions implement this interface, and
4837+
thus encapsulate a default opinion on how they map to an RFC 7807 error response.
4838+
- `ErrorResponseException` is a `RuntimeException` that implements `ErrorResponse`, which
4839+
can be raised directly or serve as a base class for other exceptions.
4840+
- {api-spring-framework}/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.html[`ResponseEntityExceptionHandler`]
4841+
provides handling for Spring MVC exceptions and for any `ErrorResponseException`.
4842+
Applications can extend this as an <<mvc-ann-controller-advice>> to enable RFC 7807 support.
4843+
4844+
`ProblemDetail` and `ErrorResponse` are supported as return values from
4845+
`@ExceptionHandler` and `@RequestMapping` controller methods. The `status` property of
4846+
`ProblemDetail` is used to set the response status, while the `instance` property is set
4847+
from the current URL path, if not already set.
4848+
4849+
The Jackson `HttpMessageConverter` returns "application/problem+json" as a preferred
4850+
choice to serialize `ProblemDetail` to JSON as part of content negotiation. If no suitable
4851+
media type is found to render a `ProblemDetail` response body, content negotiation falls
4852+
back on "application/problem+json".
4853+
4854+
Applications can extend `ProblemDetail` with non-standard fields in one of two ways:
4855+
4856+
. Add properties to the generic `properties` map in `ProblemDetail`. When using
4857+
the Jackson library, this `properties` map is unwrapped and as top level JSON
4858+
properties with the help of `ProblemDetailJacksonMixin`.
4859+
. Create a `ProblemDetail` subclass that defines the extra, non-standard fields.
4860+
Subclasses can use a protected copy constructor in order to re-create an existing
4861+
`ProblemDetail` as a subclass. This can be done centrally from an `@ControllerAdvice`
4862+
such as `ResponseEntityExceptionHandler`.
4863+
4864+
On the client side, `WebClientResponseException` and `RestClientResponseException` provide
4865+
methods that decode the response body to some target type. This is useful to decode to a
4866+
`ProblemDetail`, or to any other class, including subclasses of `ProblemDetail`.
48414867

48424868

48434869

0 commit comments

Comments
 (0)