Mapping specific constraint validator to specific ContraintViolationException in Jersey

自古美人都是妖i 提交于 2020-02-08 02:37:19

问题


I am trying to create a custom validator and mapped it into a specific constraintViolationException. The reason is I want to create different custom exception for different constraint. For example, if a user is not found in the database, a not found violationException is triggered, while if the user has invalid username, a bad response violationException will be executed instead.

@Retention(RUNTIME)
@Target({ FIELD, METHOD })
@Constraint(validatedBy = UserNotValidValidator.class)
public @interface UserIsValid { ... }

mapped into

public class ConstraintExceptionMapper implements
    ExceptionMapper<ConstraintViolationException> {
   public Response toResponse(ConstraintViolationException e) { ... }
}

To be more specific, the question is whether bean hibernate validator supports multiple constraint exception mapper.


回答1:


Exception mappers are a concept of JAX-RS not of Bean Validation. You should create one exception mapper for ConstraintViolationException and have it create different responses based on the violations contained within a given violation exception. E.g. you can examine the violated constraint type via `ConstraintViolation.getConstraintDescriptor().getAnnotation().annotationType().



来源:https://stackoverflow.com/questions/20275630/mapping-specific-constraint-validator-to-specific-contraintviolationexception-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!