Hi Jonas,
the exception is caught by an exception mapper, but by the one that is
present in jersey-bean-validation module. Exceptions thrown during bean
validation (for example your RuntimeException) are wrapped into a
ValidationException which is then handled by ValidationExceptionMapper -
this is why you get a text response from the service.
To override this behaviour, register your own exception mapper that
handles ValidationException:
@Priority(Priorities.USER - 10)
public class CustomValidationExceptionMapper implements
ExceptionMapper<ValidationException> {
...
}
HTH,
Michal
On 19.05.2014, 10:21 , Jonas Oscarsson wrote:
> Hi,
>
> We're using bean validation in Jersey. We never want to disclose
> stacktraces or any server information, so we have added a generic
> exception mapper as
>
> public class ExceptionMapper implements
> javax.ws.rs.ext.ExceptionMapper<Throwable> {
> ...
> }
>
> This works very well in general, but there seems to be an edge case
> where this ExceptionMapper is not used: in custom validators. Consider
> the following:
>
> A custom validation annotation:
>
> @Target({ FIELD, ANNOTATION_TYPE })
> @Retention(RUNTIME)
> @Constraint(validatedBy = CustomValidationValidator.class)
> public @interface CustomValidation {
> String message() default "{com.example.CustomValidation.message}";
> Class<?>[] groups() default { };
> Class<? extends Payload>[] payload() default { };
> }
>
> And corresponding validation logic:
>
> public class CustomValidationValidator implements
> ConstraintValidator<CustomValidation, String> {
> @Override
> public void initialize(CustomValidation constraintAnnotation) {
> // nothing to initialize
> }
>
> @Override
> public boolean isValid(String value, ConstraintValidatorContext
> context) {
> throw new RuntimeException("Here an exception is thrown,
> unexpectedly.");
> }
> }
>
> Using this validator, a text response is sent to the client, bypassing
> the ExceptionMapper: "HV000028: Unexpected exception during isValid
> call.".
>
> Any ideas why this happens? Is this intended behavior?
>
> Thanks / Jonas