java Javax @NotNull 注释用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43670862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:39:23  来源:igfitidea点击:

Javax @NotNull annotation usage

javaspringspring-boot

提问by sisanared

I have a simple method to get a list of documents for a given companyId. Here is the method:

我有一个简单的方法来获取给定companyId.这是方法:

@Override
public List<Documents> getDocumentList(@NotNull Integer companyId) {
    Company company = new Company(companyId);
    return this.documentRepository.findByCompany(company);
}

I wanted to use Javax validation constraints to ensure that the companyIdbeing passed in, is not null. But it seems to not have any effect, as I'm able to pass in a nullvalue, and it flows down to the findByCompanycall on the repository. I also added @Validbefore @NotNullto force validation, but that too didn't do anything.

我想使用 Javax 验证约束来确保companyId传入的不是null.但这似乎没有任何效果,因为我可以传递一个null值,并且它会向下传递到findByCompany存储库上的调用。我@Valid之前还添加@NotNull了强制验证,但这也没有做任何事情。

I could always write a couple of lines to check for a nullvalue, but wanted to use javax.validationannotations to make the code more readable and concise. Is there a way to make the annotations work on method params?

我总是可以写几行来检查一个null值,但想使用javax.validation注释来使代码更具可读性和简洁性。有没有办法让注释对方法参数起作用?

采纳答案by sisanared

To activate parameter validation, simply annotate the class with @Validated

要激活参数验证,只需使用@Validated

import org.springframework.validation.annotation.Validated;

回答by Moshe Arad

From The Java EE 6 Tutorial:

来自 Java EE 6 教程:

The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeanscomponent, such as a managed bean.

Bean 验证模型由放置在JavaBeans组件例如托管 bean)的字段、方法或类上的注释形式的约束支持。

You should place your validation of a field related to a declared bean, something like this:

您应该对与声明的 bean 相关的字段进行验证,如下所示:

@Entity
@Table(name="users")
public class BackgammonUser {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long userId;

    @Column(name="username")
    @NotBlank
    private String userName;

    @NotBlank
    private String password;

    @NotNull
    private Boolean enabled;
}

The BackgammonUser is considered to be a bean.

BackgammonUser 被认为是一个 bean。

回答by JavaBohne

If you @Injecta class with your method, its working as expected.

如果您@Inject使用您的方法创建一个类,它会按预期工作。

@Stateless
public class MyBean{ 
    @Inject
    TestClass test;
}

and

public class TestClass {
    public List<Documents> getDocumentList(@NotNull Integer companyId)
    {
        //...
    }
}

ConstraintViolationExceptionwhen you call your method with null parameter:

ConstraintViolationException当您使用空参数调用您的方法时:

WFLYEJB0034: EJB Invocation failed on component MyBean for method ...:
javax.ejb.EJBException: javax.validation.ConstraintViolationException:
1 constraint violation(s) occurred during method validation.

回答by Balasubramanian

@NotNull Annotation,

@NotNull 注解,

  1. A method should not return null.
  2. A variable (like fields, local variables, and parameters) cannot hold null value.
  1. 方法不应返回 null。
  2. 变量(如字段、局部变量和参数)不能保存空值。