Spring Rest Controller: how to selectively switch off validation

自古美人都是妖i 提交于 2019-11-29 13:42:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!