级联 bean 验证 2.0 不适用于 Map 内的嵌套对象

The*_*iaz 7 java validation bean-validation spring-boot

虽然这个问题已经得到解答,但我很感兴趣为什么@Validated需要对Map<String, @Valid Employee>.

更新 2:为了更深入地理解,我找到了这些帖子(),它们解释了@Validated激活方法级别验证所需的。在此帮助下,可以验证集合,因为它们不是要验证的 JavaBean(JSR 303)。


解决方案:我已经使用工作代码示例更新了我的代码片段和存储库。我所要做的就是用注释我的控制器并 @ValidatedEmployee. MethodValidationPostProcessor根本没有必要。

更新:我更新了我的问题并分叉了 Spring Boot Rest 示例以添加最小的 Rest API 来演示:

Github 存储库。示例值位于 README.md 内!


我有一个 Spring Boot 2 API 来存储一些员工。我可以通过一个Employee 或一个Map<String, Employee>.

@Validated //this is the solution to activate map validation
@RestController
class EmployeeController {

  @PostMapping("/employees")
  List<Employee> newEmployee(@RequestBody @Valid Employee newEmployee) {
     ...
  }

  @PostMapping("/employees/bulk")
  List<Employee> newEmployee(@RequestBody Map<String, @Valid Employee> 
  newEmployees) {
     ...
  }
}
Run Code Online (Sandbox Code Playgroud)

Employee 存在一些内部静态类,也需要验证:

public class Employee {

    @NotBlank
    public final String name;
    @Valid
    public final EmployeeRole role;

    @JsonCreator
    public Employee(@JsonProperty("name") String name,
        @JsonProperty("role") EmployeeRole role) {

        this.name = name;
        this.role = role;
    }

    // getters

    public static class EmployeeRole {

        @NotBlank
        public String rolename;

        @Min(0)
        public int rating;

        @JsonCreator
        public EmployeeRole(@JsonProperty("rolename") String rolename,
            @JsonProperty("rating") int rating) {

            this.rolename = rolename;
            this.rating = rating;
        }

        // getters
    }
}


Run Code Online (Sandbox Code Playgroud)

目前,对单个请求的验证有效,但对我的批量请求无效。据我所知,Bean 验证 2.0 应该可以实现这一点。

你知道我做错了什么吗?我需要编写自定义验证器吗?

ole*_*nik 3

要使其正常工作,您必须执行以下操作:

MethodValidationPostProcessorbean 添加到配置中

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}
Run Code Online (Sandbox Code Playgroud)

添加@Validated到您的EmployeeController

@Validated
@RestController
public class EmployeeController {}'
Run Code Online (Sandbox Code Playgroud)

添加@ValidMap或到Employee

public List<Employee> newEmployee(@RequestBody @Valid Map<String, Employee> newEmployees) {}   
public List<Employee> newEmployee(@RequestBody Map<String, @Valid Employee> newEmployees) {}
Run Code Online (Sandbox Code Playgroud)

就这样。这是完整的EmployeeController

@Validated
@RestController
public class EmployeeController {

    @PostMapping("/employees")
    public List<Employee> newEmployee(@RequestBody @Valid Employee newEmployee) {
        return Collections.singletonList(newEmployee);
    }

    @PostMapping("/employees/bulk")
    public List<Employee> newEmployee(@RequestBody @Valid Map<String, Employee> newEmployees) {
        return new ArrayList<>(newEmployees.values());
    }
}
Run Code Online (Sandbox Code Playgroud)

以及SpringBoot配置文件

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        return new MethodValidationPostProcessor();
    }

}
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。