Package org.kiwiproject.jaxrs.exception
Class ConstraintViolationExceptionMapper
java.lang.Object
org.kiwiproject.jaxrs.exception.ConstraintViolationExceptionMapper
- All Implemented Interfaces:
jakarta.ws.rs.ext.ExceptionMapper<jakarta.validation.ConstraintViolationException>
@Provider
public class ConstraintViolationExceptionMapper
extends Object
implements jakarta.ws.rs.ext.ExceptionMapper<jakarta.validation.ConstraintViolationException>
Map
ConstraintViolationException to a JSON Response.
The mapped response has status code 422 Unprocessable Entity, which technically falls under the WebDAV extensions. See the Mozilla page on 422 Unprocessable Entity here. But in reality, many web frameworks use 422 to indicate an input validation failure, which is how we are using it here.
It is important to note that the Response.Status does not contain an enum constant for 422 status,
so that Response.Status.fromStatusCode(int) will return null when given 422.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic jakarta.ws.rs.core.ResponsebuildResponse(Set<? extends jakarta.validation.ConstraintViolation<?>> violations) Given a set of constraint violations, build a 422 Unprocessable Entity response with a JSON entity.jakarta.ws.rs.core.ResponsetoResponse(jakarta.validation.ConstraintViolationException exception) Convert the givenConstraintViolationExceptionto a response containing a JSON entity.
-
Constructor Details
-
ConstraintViolationExceptionMapper
public ConstraintViolationExceptionMapper()
-
-
Method Details
-
toResponse
public jakarta.ws.rs.core.Response toResponse(jakarta.validation.ConstraintViolationException exception) Convert the givenConstraintViolationExceptionto a response containing a JSON entity.- Specified by:
toResponsein interfacejakarta.ws.rs.ext.ExceptionMapper<jakarta.validation.ConstraintViolationException>- Parameters:
exception- the exception to convert- Returns:
- a response
- See Also:
-
buildResponse
public static jakarta.ws.rs.core.Response buildResponse(Set<? extends jakarta.validation.ConstraintViolation<?>> violations) Given a set of constraint violations, build a 422 Unprocessable Entity response with a JSON entity.- Parameters:
violations- the constraint violations to convert- Returns:
- a JSON response
-