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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    jakarta.ws.rs.core.Response
    toResponse(jakarta.validation.ConstraintViolationException exception)
    Convert the given ConstraintViolationException to a response containing a JSON entity.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConstraintViolationExceptionMapper

      public ConstraintViolationExceptionMapper()
  • Method Details

    • toResponse

      public jakarta.ws.rs.core.Response toResponse(jakarta.validation.ConstraintViolationException exception)
      Convert the given ConstraintViolationException to a response containing a JSON entity.
      Specified by:
      toResponse in interface jakarta.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