简体   繁体   English

spring mvc javax.validation.UnexpectedTypeException:找不到类型为java.lang.String的验证器

[英]spring mvc javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String

I try to use the validators for the first time with Spring MVC and I always get the exception: 我尝试在Spring MVC中首次使用验证器,但总是会遇到异常:

javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String
    at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:236)
    at org.hibernate.validator.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:219)
    at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:167)
    at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:113)
    at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
    at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:334)

Here is my annoted class for validation: 这是我注释的类,用于验证:

public class AbstractEmployeDto implements Employable, Comparable<AbstractEmployeDto> {

private Set<AbstractClientDto> listeClients = new TreeSet<AbstractClientDto>();

private Long id;

@NotEmpty
private String nom;

@NotEmpty
private String prenom;

@DateTimeFormat(pattern="dd/MM/yyyy")
@Past
private String dateNaissance;

@Size(min=10)
private String telephone;

@Email
private String email;

private TypeEmploye typePersonne;

private String rue;

@Size(min=4, max=4)
private String npa;

getter and setter ...

And the controller with the annotation @valid: 带有注释@valid的控制器:

@RequestMapping(value = "/saveNew", method = RequestMethod.POST)  
    public ModelAndView saveNewEmploye(@ModelAttribute("command") @Valid AbstractEmployeDto employe,   
       BindingResult result) { 
        Map<String, Object> model = new HashMap<String, Object>();
        if(result.hasErrors()){
            return new ModelAndView("/employes/add.html", model);  
        }
...

@Past and @DateTimeFormat must be used on Date or Calendar not a String . @Past@DateTimeFormat必须在DateCalendar上使用,而不是String As bean validation doc says: 正如bean验证文档所述:

The annotated element must be a date in the past. 带注释的元素必须是过去的日期。 Now is defined as the current time according to the virtual machine. 现在根据虚拟机定义为当前时间。 The calendar used if the compared type is of type Calendar is the calendar based on the current timezone and the current locale. 如果比较类型为Calendar,则使用的日历是基于当前时区和当前区域设置的日历。

Supported types are: 支持的类型有:
java.util.Date java.util.Date
java.util.Calendar java.util.Calendar中
null elements are considered valid. 空元素被视为有效。

Porbably you need to use somehting like this: 可能您需要像这样使用:

@DateTimeFormat(pattern="dd/MM/yyyy")
@Past
private Date dateNaissance

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.validation.UnexpectedTypeException:找不到类型的验证器: - javax.validation.UnexpectedTypeException: No validator could be found for type: 找不到约束“javax.validation.constraints.NotBlank”验证类型“java.lang.String”的验证器 - No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String' HV000030:找不到约束“javax.validation.constraints.Size”验证类型“java.util.Optional”的验证器<java.lang.String> &#39; - HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Optional<java.lang.String>' 使用Spring ConstraintValidator进行枚举的javax.validation.UnexpectedTypeException - javax.validation.UnexpectedTypeException using Spring ConstraintValidator for an enumeration 没有找到约束'javax.validation.constraints.Size'验证类型'java.lang.Double'的验证器 - No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Double' javax.el.PropertyNotFoundException:在Spring-Hibernate-MySQL应用程序中的java.lang.String类型上找不到属性 - javax.el.PropertyNotFoundException: Property not found on type java.lang.String in a Spring-Hibernate-MySQL application 类需要一个找不到的“java.lang.String”类型的 bean - Class required a bean of type 'java.lang.String' that could not be found 验证错误:“找不到类型的验证器:java.lang.Integer” - Validation error: “No validator could be found for type: java.lang.Integer” 无法解决问题javax.el.PropertyNotFoundException:在类型[java.lang.String]上找不到属性[content] - Cannot solve the problem javax.el.PropertyNotFoundException: Property [content] not found on type [java.lang.String] javax.el.PropertyNotFoundException:在类型java.lang.String上找不到属性“ id” - javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM