一尘不染

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

spring-boot

尽管已经回答了这个问题,但我很感兴趣,为什么@Validated需要进行级联验证Map<String, @Valid Employee>

更新2
:为了更深入地了解,我发现了这些文章(),这些文章被解释@Validated为需要激活方法级别验证。借助于此,可以验证集合,因为它们不是经过验证的JavaBean(JSR
303)。


解决方案 :我已经使用有效的代码示例更新了代码段和存储库。我要做的就是用注释我的控制器,
@Validated并在中添加一些吸气剂EmployeeMethodValidationPostProcessor完全没有必要。

更新 :我已经更新了问题,并添加了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) {
     ...
  }
}

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

目前,可以验证单个请求,但不能验证我的批量请求。据我所知,使用Bean验证2.0应该可以做到这一点。

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


阅读 280

收藏
2020-05-30

共1个答案

一尘不染

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

MethodValidationPostProcessorbean 添加到配置

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

添加@Validated到您的EmployeeController

@Validated
@RestController
public class EmployeeController {}'

添加@ValidMapEmployee

public List<Employee> newEmployee(@RequestBody @Valid Map<String, Employee> newEmployees) {}   
public List<Employee> newEmployee(@RequestBody Map<String, @Valid Employee> newEmployees) {}

就这样。这是整个过程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());
    }
}

和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();
    }

}

希望对您有帮助。

2020-05-30