Javax @NotNull批注用法 [英] Javax @NotNull annotation usage

查看:261
本文介绍了Javax @NotNull批注用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法来获取给定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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆