In my controller I have a method for creating an entity
import javax.validation.Valid;
...
@RestController
public class Controller {
@RequestMapping(metho
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.