From fa187a7dd57293c36032aa528b1d389b5f4ff2b2 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 30 Sep 2024 13:40:28 +0300 Subject: [PATCH] Add note about ExceptionMapper in validation guide Relates to: #12006 --- docs/src/main/asciidoc/validation.adoc | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/src/main/asciidoc/validation.adoc b/docs/src/main/asciidoc/validation.adoc index 425d97b01fc7b..921c5e81de8c8 100644 --- a/docs/src/main/asciidoc/validation.adoc +++ b/docs/src/main/asciidoc/validation.adoc @@ -199,6 +199,42 @@ As you can see, we don't have to manually validate the provided `Book` anymore a If a validation error is triggered, a violation report is generated and serialized as JSON as our end point produces a JSON output. It can be extracted and manipulated to display a proper error message. +An example of such a report could be: + +[source, json] +---- +{ + "title": "Constraint Violation", + "status": 400, + "violations": [ + { + "field": "tryMeEndPointMethodValidation.book.title", + "message": "Title cannot be blank" + } + ] +} +---- + +This response is produced by Quarkus via a builtin implementation of `jakarta.ws.rs.ext.ExceptionMapper`. If the application code needs to handle `ValidationException` in some custom way, +it can provide an implementation of `jakarta.ws.rs.ext.ExceptionMapper` like so: + +[source, java] +---- +import jakarta.validation.ValidationException; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper { + + @Override + public Response toResponse(ValidationException exception) { + // TODO: implement + } +} +---- + == Service method validation It might not always be handy to have the validation rules declared at the end point level as it could duplicate some business validation.