【问题标题】:HV000030: No validator could be found for constraint (Hibernate Validator)HV000030:找不到约束的验证器(休眠验证器)
【发布时间】:2018-11-17 23:45:56
【问题描述】:

我正在关注此tutorial 以创建具有多个参数的自定义验证,但在执行@PostMapping 方法时出现以下异常:

HV000030:找不到约束“com.crimsonlogic.anotaciones.TimeRangeConstraints”验证类型“com.crimsonlogic.model.NuevoEvento”的验证器。检查''的配置

我注意到在“检查''的配置”部分,并没有告诉我任何类型的信息。

NuevoEvento 类:

@TimeRangeConstraints.List({
@TimeRangeConstraints(
    fechaEvento="fechaEvento",
    horaInicio="horaInicio",
    horaCulminacion="horaCulminacion"
)
})
public class NuevoEvento {
@NotNull(message="Como se llamara el evento?")
@Size(max=40, message="Titulo invalido")
private String titulo;
@NotNull(message="Seleccione un tipo.")
private String tipoEvento;
private String url;
@NotNull(message="Seleccione la fecha del evento")
private String fechaEvento;
@NotNull(message="A que hora inicia el evento?")
private String horaInicio;
@NotBlank(message="A que hora termina el evento?")
private String horaCulminacion;
@NotNull(message="Seleccione un salon.")
private int salonId;

public NuevoEvento() {}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getTipoEvento() {
    return tipoEvento;
}

public void setTipoEvento(String tipoEvento) {
    this.tipoEvento = tipoEvento;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getFechaEvento() {
    return fechaEvento;
}

public void setFechaEvento(String fechaEvento) {
    this.fechaEvento = fechaEvento;
}

public String getHoraInicio() {
    return horaInicio;
}

public void setHoraInicio(String horaInicio) {
    this.horaInicio = horaInicio;
}

public String getHoraCulminacion() {
    return horaCulminacion;
}

public void setHoraCulminacion(String horaCulminacion) {
    this.horaCulminacion = horaCulminacion;
}

public int getSalonId() {
    return salonId;
}

public void setSalon(int salon) {
    this.salonId = salon;
}
}

TimeRangeConstraint 注释:

@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy= TimeRangeValidator.class)
public @interface TimeRangeConstraints {
String fechaEvento();
String horaInicio();
String horaCulminacion();
String message() default "El rango de tiempo establecido no es valido o esta ocupado.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface List {
    TimeRangeConstraints[] value();
}
}

有人知道这个问题的原因吗?

【问题讨论】:

  • 请同时添加您的TimeRangeValidator 的代码。谢谢!
  • @mark_o 我已经解决了我的问题,谢谢!

标签: spring hibernate-validator


【解决方案1】:

我发现了问题。

在我的 TimeRangeValidator 类中,我的代码如下:

//HERE WAS THE PROBLEM 
public class TimeRangeValidator implements ConstraintValidator<TimeRangeConstraints,String> { 
//--------------------------------------------------------------------------------->

    private String fechaEvento;
    private String horaInicial;
    private String horaFinal;
    @Autowired
    private UsuarioSalonRepository usuarioSalon;

    @Override
    public void initialize(TimeRangeConstraints constraintAnnotation) {
        this.fechaEvento = constraintAnnotation.fechaEvento();
        this.horaInicial = constraintAnnotation.horaInicio();
        this.horaFinal = constraintAnnotation.horaCulminacion();
    }
    //// MORE AND MOREEE CODE....////

我不得不用 Object

替换字符串
public class TimeRangeValidator implements ConstraintValidator<TimeRangeConstraints,Object>

问题就消失了。

对不明白发生了什么的人进行更深入的解释

TimeRangeValidator 从表单中获取 3 个字段来执行验证逻辑。由于以下原因,之前更改的值阻止了我获取表单的 3 个字段:

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
    Object dt = new BeanWrapperImpl(value).getPropertyValue(fechaEvento);
    Object hInit = new BeanWrapperImpl(value).getPropertyValue(horaInicial);
    Object hFin = new BeanWrapperImpl(value).getPropertyValue(horaFinal);
    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date dia = form.parse(dt.toString().replaceAll("/","-"));
        return TimeUtils.detectOverlappingEvents(usuarioSalon.buscarEvento(dia), 
                hInit.toString().replaceAll("\\s","")+":00", 
                hFin.toString().replaceAll("\\s","")+":00");
    } catch (ParseException e) {
        e.printStackTrace();
        return false;
    }

使用类型对象,我可以(在 BeanWrapperImpl 的帮助下)获取表单的多个值来验证它们。

通常,类型 String(或 Integer,等等)用于验证表单的单个值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 2021-02-04
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多