Java Spring@Valid在Post控制器中不能正常工作(多部分)

Java Spring@Valid在Post控制器中不能正常工作(多部分),java,spring,spring-boot,file-upload,controller,Java,Spring,Spring Boot,File Upload,Controller,我有一个表单,它应该上传一个旁边有@modeldattribute的图像。当我尝试验证该对象中的某些约束时,就会出现问题。发生的情况是,控制器方法代码没有执行,我只是抛出异常约束 Field error in object 'employee' on field 'details.name': rejected value []; codes [Size.employee.details.name,Size.details.name,Size.name,Size.java.lang.String

我有一个表单,它应该上传一个旁边有@modeldattribute的图像。当我尝试验证该对象中的某些约束时,就会出现问题。发生的情况是,控制器方法代码没有执行,我只是抛出异常约束

Field error in object 'employee' on field 'details.name': rejected value []; codes [Size.employee.details.name,Size.details.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.name,details.name]; arguments []; default message [details.name],20,1]; default message [*Must be between 1 and 20 letters]
Field error in object 'employee' on field 'details.lastName': rejected value []; codes [NotBlank.employee.details.lastName,NotBlank.details.lastName,NotBlank.lastName,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.lastName,details.lastName]; arguments []; default message [details.lastName]]; default message [*Please provide a valid last name]
Field error in object 'employee' on field 'details.name': rejected value []; codes [NotBlank.employee.details.name,NotBlank.details.name,NotBlank.name,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.name,details.name]; arguments []; default message [details.name]]; default message [*Please provide a valid first name]
Field error in object 'employee' on field 'jobOccupation': rejected value []; codes [NotBlank.employee.jobOccupation,NotBlank.jobOccupation,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.jobOccupation,jobOccupation]; arguments []; default message [jobOccupation]]; default message [*Employee must have an occupation]
Field error in object 'employee' on field 'details.lastName': rejected value []; codes [Size.employee.details.lastName,Size.details.lastName,Size.lastName,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.details.lastName,details.lastName]; arguments []; default message [details.lastName],20,1]; default message [*Must be between 1 and 20 letters]
如果填充了所有字段(不包括图像,因为它没有任何约束),一切都正常,但我需要验证

这是我的表格

<form class="mar-clearfix"  th:action="@{/admin/staff/create/employee}" id="emp-form"
                                    th:object="${employee}" enctype="multipart/form-data"
                                     method="post">
                                        <div class="col s12 xl6">
                                            <div class="field-wrapper">
                                            <p th:if="${#fields.hasErrors('details.name')}" th:errors="*{details.name}" class="error"></p>
                                                <div class="input-field">
                                                    <input type="text" th:field="*{details.name}" id="emp-f-name">
                                                    <label for="emp-f-name">First Name</label>
                                                </div>
                                            </div>
                                            <div class="field-wrapper">
                                            <p th:if="${#fields.hasErrors('details.lastName')}" th:errors="*{details.lastName}" class="error"></p>
                                                <div class="input-field">
                                                    <input type="text" th:field="*{details.lastName}" id="emp-l-name">
                                                    <label for="emp-l-name">Last Name</label>
                                                </div>
                                            </div>
                                            <div class="field-wrapper">
                                                <div class="input-field">
                                                    <input type="email" th:field="*{email}" id="emp-email">
                                                    <label for="emp-email">Email</label>
                                                </div>
                                            </div>
                                            <div class="field-wrapper">
                                                <div class="input-field">
                                                    <label for="emp-birthday">Birthday</label>
                                                    <input type="text" id="emp-birthday" th:field="*{details.dateOfBirth}" class="datepicker">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col s12 xl6">
                                            <div class="field-wrapper">
                                            <p th:if="${#fields.hasErrors('jobOccupation')}" th:errors="*{jobOccupation}" class="error"></p>
                                                <div class="input-field">
                                                    <input type="text" th:field="*{jobOccupation}" id="emp-job">
                                                    <label for="emp-job">Job Ocupation</label>
                                                </div>
                                            </div>
                                            <div class="field-wrapper">
                                                <div class="input-field">
                                                    <select th:field="*{details.gender}">
                                                        <option th:value="${T(com.mypackage.Genders).MALE}">Male</option>
                                                        <option th:value="${T(com.mypackage.Genders).FEMALE}">Female</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="field-wrapper" style="margin-top:23px">
                                                <div class="input-field">
                                                    <select th:field="*{roles}" multiple>
                                                        <option th:value="null" disabled selected>Select roles</option>
                                                      <option th:each="item : ${roles}"  th:value="${{item}}" 
                                    th:text="${item.role}" ></option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="field-wrapper" style="margin-top:23px">
                                                <div class="file-field input-field">
                                                    <div class="btn">
                                                        <span>Profile img</span>
                                                        <input type="file" name="image">
                                                    </div>
                                                    <div class="file-path-wrapper">
                                                        <input class="file-path validate" type="text">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>

我尝试在@modeldattribute之前添加@RequestPart,但似乎没有帮助。另外,当我从表单中删除@RequestPart image和enctype时,一切正常,错误被正确写入相应的th:error字段。我的问题是,当试图用表单上传图像时,为什么会发生这种情况,有没有办法解决?谢谢。

使用
验证器
bean在控制器方法内部运行验证:

public class MyController
    /**
     * The system wide JSR-303 validator, which checks if annotations ({@link NotNull}, {@link NotBlank}, etc. ) are fulfilled.
     */
    @Autowired
    protected Validator validator;

    @PostMapping(value = "/admin/staff/create/employee")
    public ModelAndView createEmployee(@ModelAttribute ("employee") Employee employee, @RequestPart("image")MultipartFile image, BindingResult result) {

        validator.validate(employee, result);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("admin/admin_layout");
        ...
        if (result.hasErrors()) {
            System.out.println("has errors");
            return mav;
        }
    }
}

非常感谢,我们成功地连接了org.springframework.validation.Validator,它可以正常工作了!
public class MyController
    /**
     * The system wide JSR-303 validator, which checks if annotations ({@link NotNull}, {@link NotBlank}, etc. ) are fulfilled.
     */
    @Autowired
    protected Validator validator;

    @PostMapping(value = "/admin/staff/create/employee")
    public ModelAndView createEmployee(@ModelAttribute ("employee") Employee employee, @RequestPart("image")MultipartFile image, BindingResult result) {

        validator.validate(employee, result);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("admin/admin_layout");
        ...
        if (result.hasErrors()) {
            System.out.println("has errors");
            return mav;
        }
    }
}