- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
关于 this doc我知道如果我的 GroupService 实现了 GroupManager 并覆盖了它的方法,那么我就无法使用验证约束进行注释,因为 Hibernate Validator 不允许它(结果被称为 Liskov substitution principle )。我的意思是做类似的事情
public class GroupService implements GroupManager{
@Override
public List<String> findUsersInGroup(@NotNull String groupName) {
...
}
}
然后将引发 ConstraintDeclarationException
,对吧?所以解决方案显然是将这些约束放在接口(interface)上,但在那种情况下:
GroupManager
属于 Spring Security
)。那我该怎么做呢?Hibernate Validator
迫使我“弄脏”界面最佳答案
I could probably not have access to modificate interface (as this case where GroupManager belongs to Spring Security). How should i do in this case?
您可以使用 xml 配置,因为 JSR-303(Bean 验证)支持它。例如
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>org.springframework.security.provisioning</default-package>
<bean class="GroupManager" ignore-annotations="true">
<method name="findUsersInGroup">
<parameter type="java.lang.String">
<constraint annotation="javax.validation.constraints.NotNull"/>
</parameter>
</method>
</bean>
</constraint-mappings>
参见hibernate doc中的xml配置章节.
I have the thought that these validation constraints should not affect the interface since they are part of its implementation
正如 hibernate 文档所说
When a method is overridden in sub-types method parameter constraints can only be declared at the base type. The reason for this restriction is that the preconditions to be fulfilled by a type's client must not be strengthened in sub-types (which may not even be known to the base type's client).
方法的先决条件不应该被子类型强化。如果您说您的子类型 GroupService
不允许 null
参数,您可能会加强前提条件。例如。使用 GroupManager
的客户端可能不知道(也不应该知道)它是一个 GroupService
。 GroupManager
接口(interface)没有对参数做任何限制。因此,如果您这样做,就会破坏以前合法的客户代码。这违反了 Liskov substitution principle .
遗憾的是 GroupManager
javadoc 没有限制参数。因此,法律实现必须处理所有情况。
一般来说...当我定义方法时我应用这些规则
null
。这些简单的规则帮助我为客户创建一个清晰的 API。
编辑
i think possible that i have impl "A" and impl "B" (both implementing same interface) where impl "A" has more (and different) validations than "B"
如果是这样,它们就没有相同的接口(interface)或 API。您看到的是两者具有相同的方法签名。但是两个相等的方法签名不能共享同一个 api 契约。当我谈论接口(interface)时,我会想到契约而不仅仅是签名。想象一下以下界面:
public class Container {
/**
* @return a non-empty collection of elements.
*/
public Collection<Element> getElements();
}
在这种情况下,合法的客户端代码将是
Container container = ....;
Element firstElement = container.getElements().iterator().next();
因为合约说它返回一个非空集合。
如果我们更改 javadoc 并因此更改后置条件...
/**
* @return a collection of elements.
*/
public Collection<Element> getElements();
以前合法的客户端代码将不再有效。
我只是做了这个例子来向你展示契约和方法签名之间的区别。
可以找到详细很好的解释here .
As GroupManager javadoc doesn't restrict the parameter ,a legal impl must handle every situation'? Is that validating params inside method?
是的。如果接口(interface)不对参数添加任何限制,则实现必须处理每个状态,因为客户端可能会传递参数在任何状态。
关于java - 如何使用 Hibernate Validator 验证重写的方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461971/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!