级联bean验证2.0不适用于Map中的嵌套对象 [英] Cascaded bean validation 2.0 not working with nested object inside Map

查看:92
本文介绍了级联bean验证2.0不适用于Map中的嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Although, this question has been answered I'm interested why @Validated is needed for a working cascaded validation of Map<String, @Valid Employee>.

更新2 :为了更深入地了解,我发现了这些帖子(一个两个三个),这说明需要激活@Validated来激活方法级别验证.借助于此,可以验证集合,因为它们不是经过验证的JavaBean(JSR 303).

Update 2: For some deeper understanding I've found those posts (One,Two and Three), which explains, that @Validated is neeeded to activate method level validation. With the help of this, collections can be validated, due they are no JavaBeans which are validated instead (JSR 303).

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

Solution: I've updated my code snippets and my repository with working code examples. All I have to do is to annotate my controller with @Validated and add some getters in Employee. MethodValidationPostProcessor is not necessary at all.

更新:我已经更新了我的问题,并派生了Spring Boot Rest示例,以添加一个最小的Rest API来演示:

Update: I've updated my question and forked Spring Boot Rest example to add a minimal Rest API to demonstrate:

Github回购. 示例值在README.md中!

Github Repo. The example values are inside README.md!

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

I've got an Spring Boot 2 API to store some employees. I can pass either one Employee or either a 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存在一些内部静态类,这些类也需要进行验证:

Employee exists of some inner static classes which also needs to be validated:

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应该可以做到这一点.

For now, validation for single requests are working but not for my bulk requests. As far as i know this should be possible with Bean validation 2.0.

您知道我做错了什么吗?我需要写一个自定义验证器吗?

Do you know what I've did wrong? Do i need to write a custom validator?

推荐答案

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

To make it working you have to do following:

MethodValidationPostProcessor bean添加到配置中

Add MethodValidationPostProcessor bean to configuration

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

@Validated添加到您的EmployeeController

@Validated
@RestController
public class EmployeeController {}'

@Valid添加到MapEmployee

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配置文件

And SpringBoot configuration file

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

希望它对您有帮助.

这篇关于级联bean验证2.0不适用于Map中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆