Exception handling

Applications often raise exceptions while processing requests. These exceptions must be handled properly, otherwise they will send back a HTTP 500 error to the requesting client. SpringWebFlux supports exception handling by creating methods that are annotated with @ExceptionHandler.  These exception handlers can have the exception raised as an argument:

@RestControllerpublic class ReactiveController { @ExceptionHandler public String handleError(RuntimeException ex) { // ... }}

An exception handler can have the same return types as request methods. Optionally, we would like to set the HTTP status as part of exception handling, but Spring does not do this automatically. It can be accomplished by returning a ResponseEntity ...

Get Hands-On Reactive Programming with Reactor now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.