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

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

我第一次尝试在 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)

这是我注释的 class 用于验证:

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 ...

以及带有注解@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@DateTimeFormat 必须用于 DateCalendar 不是 String。正如 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.

Supported types are:
java.util.Date
java.util.Calendar
null elements are considered valid.

可能你需要使用这样的东西:

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