Javax @NotNull批注用法 [英] Javax @NotNull annotation usage
问题描述
我有一个简单的方法来获取给定companyId
的文档列表.方法如下:
I have a simple method to get a list of documents for a given companyId
. Here is the method:
@Override
public List<Documents> getDocumentList(@NotNull Integer companyId) {
Company company = new Company(companyId);
return this.documentRepository.findByCompany(company);
}
我想使用Javax验证约束来确保传入的companyId
不是null
.但这似乎没有任何效果,因为我可以传递null
值,并且它会向下传递到存储库中的findByCompany
调用.我还在@NotNull
之前添加了@Valid
来强制验证,但这也没有做任何事情.
I wanted to use Javax validation constraints to ensure that the companyId
being passed in, is not null
. But it seems to not have any effect, as I'm able to pass in a null
value, and it flows down to the findByCompany
call on the repository. I also added @Valid
before @NotNull
to force validation, but that too didn't do anything.
我总是可以写几行代码来检查null
值,但是我想使用javax.validation
注释使代码更易读和简洁.有没有办法使注释在方法参数上起作用?
I could always write a couple of lines to check for a null
value, but wanted to use javax.validation
annotations to make the code more readable and concise. Is there a way to make the annotations work on method params?
推荐答案
要激活参数验证,只需使用@Validated
To activate parameter validation, simply annotate the class with @Validated
import org.springframework.validation.annotation.Validated;
这篇关于Javax @NotNull批注用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!