Spring Rest Controller: how to selectively switch off validation

前端 未结 1 542
既然无缘
既然无缘 2020-12-20 17:06

In my controller I have a method for creating an entity

import javax.validation.Valid;
...
@RestController
public class Controller {

  @RequestMapping(metho         


        
相关标签:
1条回答
  • 2020-12-20 18:00

    Short answer: Use Validation Groups:

    @NotEmpty(groups = SomeCriteria.class)
    private String field1;
    

    And reference your intended group in method handler parameters:

    public ResponseEntity<?> create(@Validated(SomeCriteria.class) @RequestBody RequestDTO requestDTO)
    

    In the above example, validations in the SomeCriteria group will be applied and others going to be ignored. Usually, these validation groups are defined as empty interfaces:

    public interface SomeCriteria {}
    

    You can read more about these group constraints in Hibernate Validator documentation.

    0 讨论(0)
提交回复
热议问题