spring-boot-starter-validation 中@Valid 无法校验List

 这是pom中的jar包依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

下面是entity

@NotBlank(message = "TPM单号不能为空")
private String tpmNo;

下面是controller

@RequestMapping(value = "/save", method = RequestMethod.POST)
public Message saveProductApplication(@Valid @RequestBody ProductApplication productApplication) {
        productApplicationMapper.insert(productApplication);
        return new Message("操作成功!");
    }

然后成功了

如果将接收的实体类换成List,如下

 

 @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Message saveProductApplication(@Valid @RequestBody List<ProductApplication> productApplications) {
        productApplications.forEach(productApplication -> productApplicationMapper.insert(productApplication));
        return new Message("操作成功!");
    }

那么运行结果就是

@Valid 没有生效,直接跳过,报错是数据库字段不能为空的报错了!

why?

求大佬解答。 

查看全部
ClareHuiHuang
小辉哥MAX
2020/11/26 14:25
  • spring
  • java
  • hibernate
  • 点赞
  • 收藏
  • 回答
    私信

2个回复