简体   繁体   English

找不到类型的验证器:java.lang.String

[英]No validator could be found for type: java.lang.String

Trying to validate the data entered in my registration form. 尝试验证在我的注册表单中输入的数据。

Error Message: org.springframework.web.util.NestedServletException: Request processing failed; 错误消息:org.springframework.web.util.NestedServletException:请求处理失败; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.String. 嵌套异常是javax.validation.UnexpectedTypeException:HV000030:找不到类型为:java.lang.String的验证器。

Model Class: 型号类:

@Configuration
public class Student {

    private int studentId;

    @NotNull(message = "First name should not be empty")
    @Size(min=03, max=20, message = "Minimum 3 to  Maximum 20 characters are allowed")
    @Pattern(regexp="/^[a-zA-Z]*$/")
    private String firstName;

    @NotNull(message = "Last name should not be empty")
    @Size(min=03, max=20, message = "Minimum 3 to  Maximum 20 characters are allowed")
    @Pattern(regexp="/^[a-zA-Z]*$/")
    private String lastName;

    @NotNull(message = "Display name should not be empty")
    @Pattern(regexp="^[A-Za-z0-9]*$", message = "Please enter a valid display name")
    @Size(min=05, max=15, message="Minimum 3 to Maximum 15 characters are allowed")
    private String displayName;

    @NotNull(message = "Date of birth should not be empty")
    @DateTimeFormat(pattern="yyyy/MM/dd/")
    @Past (message="Only the past date is valid")
    private String dateOfBirth;

    @NotNull(message = "Email should not be empty")
    @Email
    private String email;

    @NotNull(message = "Password should not be empty")
    @Pattern.List({
        @Pattern(regexp = "(?=.*[0-9])",    message = "Password must contain one digit."),
        @Pattern(regexp = "(?=.*[a-z])",    message = "Password must contain one lowercase letter."),
        @Pattern(regexp = "(?=.*[a-z])",    message = "Password must contain one lowercase letter."),
        @Pattern(regexp = "(?=\\S+$)",  message = "Password must contain no whitespace.")
    })
    private String password;

    @NotNull(message="Contact shouldnot be empty")
    @Pattern(regexp = "[0-9]+", message="Contact should only contain numbers")
    @Size(min=10, max=10)
    private String contact;

    @NotNull(message="Select atleast one skill from the list")
    private List<String> studentSkills;

    // Getter and Setter for studentId
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    // Getter and Setter for firstName
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    // Getter and Setter for lastName
    public String getLastName() {   
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    // Getter and Setter for displayName
    public String getDisplayName() {
        return displayName;
    }
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    // Getter and Setter for DateOfBirth
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    // Getter and Setter for email
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    // Getter and Setter for password
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    // Getter and Setter for contact
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }

    // Getter and Setter for studentSkills
    public List<String> getStudentSkills() {
        return studentSkills;
    }
    public void setStudentSkills(List<String> studentSkills) {
        this.studentSkills = studentSkills;
    }
}

Controller: 控制器:

@RequestMapping(value="/registration", method=RequestMethod.GET)
public ModelAndView getRegForm() {
    ModelAndView mv =   new ModelAndView("registration","student",new Student());
    return mv;
}

@RequestMapping(value="/submitregistrationform", method=RequestMethod.POST)
public ModelAndView submitRegForm(@Valid @ModelAttribute("student") Student student, BindingResult result) {
    ModelAndView registrationView = new ModelAndView("registration");
    if(result.hasErrors()) {
        return registrationView;
    } else {
        st.insertOrUpdate(student);
        ModelAndView mv =   new ModelAndView("login");
        return mv;
    }
}

Any suggestions on how to sort the exception ? 有关如何对异常进行排序的任何建议? I tried length for String just to see if it works. 我尝试了String的长度,看它是否有效。 But it didn't. 但事实并非如此。

The validations given for date are incorrect. 日期给出的验证不正确。 As the date is in String format and the annotation checks for DATE datatype it is throwing a exception. 由于日期是字符串格式,并且注释检查DATE数据类型,因此它会引发异常。 Changed the data type of date from String to DATE and it started working. 将日期的数据类型从String更改为DATE,它开始工作。

暂无
暂无

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

相关问题 spring mvc javax.validation.UnexpectedTypeException:找不到类型为java.lang.String的验证器 - spring mvc javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.String 如何解决需要找不到的“java.lang.String”类型的bean - How to resolve required a bean of type 'java.lang.String' that could not be found 找不到能够从 [java.lang.String] 类型转换为 [java.util.LinkedHashMap] 类型的转换器<java.lang.String, java.lang.String> ] - No converter found capable of converting from type [java.lang.String] to type [java.util.LinkedHashMap<java.lang.String, java.lang.String>] 找不到类型的验证器:java.lang.Integer - No validator could be found for type: java.lang.Integer com.project.api.graphql.service.GraphQLService 中构造函数的参数 0 需要一个无法找到的“java.lang.String”类型的 bean - Parameter 0 of constructor in com.project.api.graphql.service.GraphQLService required a bean of type 'java.lang.String' that could not be found SpelEvaluationException:EL1004E:方法调用:在java.lang.String类型上找不到方法getValue(java.lang.String) - SpelEvaluationException: EL1004E: Method call: Method getValue(java.lang.String) cannot be found on java.lang.String type java.lang.IllegalArgumentException:找到了多个参数类型候选:[java.lang.String]和[java.lang.Long] - java.lang.IllegalArgumentException: Found more than one parameter type candidate: [java.lang.String] and [java.lang.Long] 如何修复“类型不匹配。必需:java.lang.String Found:kotlin.String” - How to fix "Type mismatch. Required: java.lang.String Found: kotlin.String" 将参数传递给Bean时,没有找到依赖项的类型为[java.lang.String]的合格Bean - No qualifying bean of type [java.lang.String] found for dependency when pass parameter to the Bean 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM