我想验证不同长度的字符串,并开始实现自定义约束:
@Constraint(validatedBy = {BarcodeValidator.class})
public @interface Barcode {
String message() default "{validation.Barcode.message}";
int[] lengths();
}
Run Code Online (Sandbox Code Playgroud)
验证者:
public class BarcodeValidator implements ConstraintValidator<Barcode, String> {
private List<Integer> lengths;
@Override
public void initialize(Barcode barcode) {
lengths = new ArrayList<>(barcode.lengths().length);
for (int l : barcode.lengths()) lengths.add(l);
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return value != null && lengths.contains(value.length());
}
}
Run Code Online (Sandbox Code Playgroud)
我想要一个自定义消息: 条形码必须是12/23/45字符长!
我已经想通了我可以在消息中使用注释参数,所以我想做类似的事情:
validation.Barcode.message = The barcode must be ${'/'.join(lengths)} character long!
Run Code Online (Sandbox Code Playgroud)
这样的事情是可能的还是我应该以其他方式做到这一点?
(PS:我在这里的第一个问题.)
这两个任务在Gradle中究竟有什么区别:
task sampleTask {
String myFile = "sample.txt"
delete myFile
}
task sampleTask {
ext.myFile = "sample.txt"
delete myFile
}
Run Code Online (Sandbox Code Playgroud)
它们基本相同还是它们有所不同?
我已经使用 Hibernate Search 几个月了,但我仍然无法消化它带来的相关性。我对它返回的结果总体满意,但即使是最简单的测试也不能满足我的期望。
第一个测试是使用术语频率(tf)。数据:
我得到的结果:
我真的对这种得分效应感到困惑。我的查询很复杂,但是由于这个测试没有涉及任何其他字段,它可以简化如下:booleanjunction.should(phraseQuery).should(keywordQuery).should(fuzzyQuery)
我有如下分析器:
StandardFilterFactory
LowerCaseFilterFactory
StopFilterFactory
SnowballPorterFilterFactory for english
Run Code Online (Sandbox Code Playgroud)
我认为我的问题是因为依赖冲突。我将验证添加到我的项目中,现在它已经失败了。
遵循其他一些 SO 帖子让我相信存在冲突。我跑了mvn dependency:tree -Dverbose -Dincludes=javax.validation
,这给了我:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ appointments ---
[INFO] com.nakedwines.www:appointments:war:1.0-SNAPSHOT
[INFO] +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] \- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile
[INFO] \- (javax.validation:validation-api:jar:1.0.0.GA:compile - omitted for conflict with 1.1.0.Final)
看起来我使用了错误版本的 javax.validation。我已经从我的 pom 中删除了它,现在有:
<!--validation-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法摆脱令人反感的冲突。我已经清理过(我认为会删除任何未使用的库)。
我还按照 apache 站点上的建议尝试了清除。清除后错误仍然存在。
我的完整堆栈跟踪(来自异常):
INFO: Mapped "{[/test/helloooooo],methods=[GET]}" onto public java.lang.String com.nakedwines.controller.HelloController.helloooooo(org.springframework.ui.Model)
Oct 17, 2015 6:46:23 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating …
Run Code Online (Sandbox Code Playgroud) 我的 WebService 基于 Spring 4,并且我正在使用 Hibernate Validator(超越 MethodValidationPostProcessor)。我的问题是我有我的ClientService
界面及其实现。因此,我在实现上放置了 Bean Validation 约束,这迫使我将该约束放置在接口上(抛出ConstraintDeclarationException
)(或在两者中)。
关于这件事我想知道两件事:
提前致谢!问候
validation ×3
java ×2
spring ×2
gradle ×1
hibernate ×1
lucene ×1
properties ×1
search ×1
solr ×1
variables ×1