Search code examples
javaresteasybean-validationjava-ee-8wildfly-21

Wildfly 21 – how to customize returns for Bean Validation exceptions


I've tried registering custom exception mappers in multiple ways:

@Provider
public class ConstraintViolationMapper implements ExceptionMapper<ConstraintViolationException> {
    @Override
    public Response toResponse(ConstraintViolationException exception) {
        ...
    }
}

@Provider
public class ConstraintViolationMapper implements ExceptionMapper<ResteasyViolationException> {
    @Override
    public Response toResponse(ResteasyViolationException exception) {
        ...
    }
}

@Provider
public class ConstraintViolationMapper implements ExceptionMapper<ValidationException> {
    @Override
    public Response toResponse(ValidationException exception) {
        ...
    }
}

But all that happens is the default behaviour by the ResteasyViolationExceptionMapper. My custom ExceptionMapper is never called. I don't know what else to try.


Solution

  • I don't know why it didn't work when I first tried it, but this works

    @Provider
    public class ConstraintViolationMapper implements ExceptionMapper<ValidationException> {
        @Override
        public Response toResponse(ValidationException exception) {
            ...
        }
    }