如何在Spring Boot中使用Spring Validation来验证嵌套对象?

时间:2019-07-02 16:42:13

标签: java spring-boot spring-data-jpa spring-validator

我正在开发一个项目,其中的许多要求之一就是自定义验证两个对象,为此我将两个对象从一种形式传递给控制器​​。     我经常收到关于我不知道如何管理或解决它的特定语句的异常。

package com.practive.course.configure;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.practive.course.model.Credentials;
import com.practive.course.model.Student;

@Component
public class StudentValidator implements Validator {

    private final Validator credentialValidator;

    public StudentValidator(Validator credentialValidator) {
        if (credentialValidator == null) {
            throw new IllegalArgumentException(
              "The supplied [Validator] is required and must not be null.");
        }
        if (!credentialValidator.supports(Credentials.class)) {
            throw new IllegalArgumentException(
              "The supplied [Validator] must support the validation of [Address] instances.");
        }
        this.credentialValidator = credentialValidator;
    }

    /**
    * This Validator validates Customer instances, and any subclasses of Customer too
    */
    public boolean supports(Class clazz) {
        return Student.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
         Student student = (Student) target;

         System.out.println(student.getMyname());
         System.out.println("below");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "NotEmpty"); //this line generates the exception
        System.out.println("above");
        //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "field.required");




        try {
            errors.pushNestedPath("credentials");
            ValidationUtils.invokeValidator(this.credentialValidator, student.getCred(), errors);
        } finally {
            errors.popNestedPath();
        }
    }
}


@PostMapping("/registration")
    public String postRegister(Student student,Credentials credential,BindingResult bindingResult,RedirectAttributes redirect) {

        try{
            validator.validate(student,bindingResult);
        }catch(Exception e) {
            System.out.println("problem is here "+e.getMessage());
        }

        try {
            if(bindingResult.hasErrors()) {
                return "registration";
            }
        }catch(Exception e) {
            System.out.println("problem is in biding "+e.getMessage());
        }
        appService.registerStudent(student,credential);
        return "redirect:/login";
    }

package com.practive.course.model;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String myname;
    private String mobile;
    private String email;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="credid")
    private Credentials cred;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMyname() {
        return myname;
    }

    public void setMyname(String myname) {
        this.myname = myname;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Credentials getCred() {
        return cred;
    }

    public void setCred(Credentials cred) {
        this.cred = cred;
    }

}


package com.practive.course.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Credentials {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String username;
    private String password;
    private String usermobile;
    private Role roles;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsermobile() {
        return usermobile;
    }
    public void setUsermobile(String usermobile) {
        this.usermobile = usermobile;
    }
    public Role getRoles() {
        return roles;
    }
    public void setRoles(Role roles) {
        this.roles = roles;
    }


}

stacktrace:

Invalid property 'email' of bean class [com.practive.course.model.Credentials]: Bean property 'email' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

当我尝试验证时,正在生成上述异常

1 个答案:

答案 0 :(得分:0)

stacktrace显示Invalid property 'email' of bean class [com.practive.course.model.Credentials],validator方法正在尝试获取Credentials对象的email属性,而从您的代码中可以看出,“ email”是Student类的一部分。