验证器错误(无法为类型:java.util.Date找到验证器)。 [英] Error with validator (No validator could be found for type: java.util.Date.)
问题描述
我的朋友们,
我有上课...
@NotBlank(message =timesheet.cadastro.horainicio.obrigatorio)
@Temporal(TemporalType.TIME)
@Column(name =INICIO,nullable = false)
private日期时间;
并且,在我的测试中(groovy),我将null设置为horaInicio:
defSalvar um timesheet sem hora inicio(){
given:um timesheet sem data
def usuario = (Dias.class,1L)作为Dias
def timeSheet = criarTimeSheet(usuario,)获得(Usuario.class,1L)作为Usuario
def dias = sessionFactory.getCurrentSession dias)as TimeSheet
timeSheet.horaInicio = null
当:buscar na base
def timeSheetPersistido = timeSheetService.salvar(timeSheet)
then:retorna uma lista de timeheet
def erro = thrown(ConstraintViolationException)
timesheet.cadastro.horainicio.obrigatorio.equals(erro.getConstraintViolations()。asList()。get(0).getMessage())
}
但我有错误:
预期的异常javax.validation.ConstraintViolationException,但得到了javax.val idation.UnexpectedTypeException
at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:79)
at br.com.infowhere.service.TimeSheetServiceIt.Salvar um timesheet sem hora inicio(TimeSheetServiceIt.groovy: 80)
引起:javax.validation.UnexpectedTypeException:HV000030:找不到类型为java.util.Date的验证器。
任何人都可以帮助我?
谢谢
正如文档中所述, @NotBlank 适用于String类型。没有java.util.Date的概念是空白的。它可以为null或不为null。
确认注释的字符串不为空或空。与NotEmpty的
差异在于,尾随空格将忽略
。
如果目标是检查值不应为空,应使用 @NotNull 。根据文档:
注释的元素不能为空。接受任何类型。
My friends,
I have class with ...
@NotBlank(message = "timesheet.cadastro.horainicio.obrigatorio")
@Temporal(TemporalType.TIME)
@Column(name = "INICIO", nullable = false)
private Date horaInicio;
And, in my test (groovy) I put null to "horaInicio" :
def "Salvar um timesheet sem hora inicio"() {
given:"um timesheet sem data"
def usuario = sessionFactory.getCurrentSession().get(Usuario.class,1L) as Usuario
def dias = sessionFactory.getCurrentSession().get(Dias.class,1L) as Dias
def timeSheet = criarTimeSheet(usuario,dias) as TimeSheet
timeSheet.horaInicio = null
when:"buscar na base"
def timeSheetPersistido = timeSheetService.salvar(timeSheet)
then:"retorna uma lista de timesheet"
def erro = thrown(ConstraintViolationException)
"timesheet.cadastro.horainicio.obrigatorio".equals(erro.getConstraintViolations().asList().get(0).getMessage())
}
but I have error:
Expected exception javax.validation.ConstraintViolationException, but got javax.validation.UnexpectedTypeException
at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:79)
at br.com.infowhere.service.TimeSheetServiceIt.Salvar um timesheet sem hora inicio(TimeSheetServiceIt.groovy:80)
Caused by: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.util.Date.
Anybody can help me ?
thanks
As also said in documentation, @NotBlank is for String type. There is no concept of java.util.Date being blank. It can be null or not null.
Validate that the annotated string is not null or empty. The difference to NotEmpty is that trailing whitespaces are getting ignored.
If goal is to check is value not null, @NotNull should be used. According documentation:
The annotated element must not be null. Accepts any type.
这篇关于验证器错误(无法为类型:java.util.Date找到验证器)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!