简体   繁体   English

Guava可选与Bean验证

[英]Guava Optional versus Bean Validation

We are implementing a new Java EE Project and are not very familiar with this technology so far. 我们正在实施一个新的Java EE项目,到目前为止还不是很熟悉这项技术。 So we much have to learn... Currently we are using Guava Optional in BOs to implement fields that are not mandatory. 所以我们必须要学习......目前我们在BO中使用Guava Optional来实现非强制性的字段。

We have a lot of discussion around this class and I don't see the advantages so far. 我们围绕这个课程进行了很多讨论,到目前为止我还没有看到它们的优点。 I would prefer to using Bean Validation with the annotation @NotNull to indicate if a field is set always or not. 我更喜欢使用带有注释@NotNull的Bean Validation来指示是否始终设置字段。 Of course, that it's set, you can only be sure after validation. 当然,它已设置,您只能在验证后确定。

Now to my question. 现在问我的问题。 Does eclipse don't show the Bean Validation Annotations? eclipse不显示Bean Validation Annotations吗? So you would better see, what you can be sure. 所以你最好看,你可以肯定的。 Is there a plugin that would help? 有插件可以帮忙吗?

Guava/Java 8 Optional should be used to enforce the following convention: if, according to bussiness logics, some function might return null value, then we wrap it inside Optional, so our intention is clear - users has to check for value presence. 应该使用Guava / Java 8可选来强制执行以下约定:如果根据商业逻辑,某些函数可能返回null值,那么我们将它包装在Optional中,因此我们的意图很明确 - 用户必须检查值是否存在。 If a function returns non-optional object, we assume that is never can be null and users of that function do not have to do null checks. 如果函数返回非可选对象,我们假设永远不能为null,并且该函数的用户不必进行空检查。 Hence the code will be much cleaner. 因此代码会更清晰。

Since Optional is a part of Java 8 now, I guess this will become standard approach to handle nulls. 由于Optional现在是Java 8的一部分,我想这将成为处理空值的标准方法。

Some IDEs provide special annotation for the sake of showing hints. 某些IDE为了显示提示而提供特殊注释。 Eclipse has @NonNull annotation, similarly IntelliJ IDEA has @Nullable/@NotNull annotations, but they have nothing to do with Bean Validation annotation. Eclipse有@NonNull注释,类似IntelliJ IDEA有@Nullable / @NotNull注释,但它们与Bean Validation注释无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM