这是我的问题的一个示例:
1)我有一个XML文件,其中定义了我的发票规则
<rules>
..
<price>
<Required>True</Required>
<MinValue>0.1</MinValue>
<price>
..
</rules>
2)我在Java中有一个发票对象(重要:,应保持在(普通)中。不应包含任何注释或任何内容。)
public class Invoice implements Serializable {
private String details;
private String tax;
private String recipient;
private double price;
//getter and setter
}
3)至少我有一个验证器对象
public class Validator(){
public boolean validate(String xmlfile, Invoice i) {
//todo: Invoice i should be validated with xmlfile
}
}
我需要的:
验证程序应使用内部规则获取XML文件
例如
<price>
<Required>True</Required>
<MinValue>0.1</MinValue>
<price>
并使用该规则来验证Invoice java对象。
有什么我可以使用的良好框架吗?还是教程?办法?
任何帮助都非常感谢。
谢谢。
参考方案
您可以使用Hibernate Validator(它是JSR 303的实现)。您可以使用XML编写约束,如下所示
<field name="x2" ignore-annotations="true" > <!-- XML is stronger -->
<constraint annotation="javax.validation.constraints.Min">
<element name="value">2</element>
</constraint>
</field>
完整且不错的教程here
Java中的<<或>>>是什么意思? - javaThis question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…
菱形运算符<>是否等于<?> - java我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…
使用单词拆分字符串,但将单词保留在每个数组索引中 - java以下代码, String string = "<xml attributes>some xml code</xml>" + "<xml attributes>some xml code</xml>" + "<xml attributes>some x…
与哪些运算符>>兼容 - java我这里没有什么代码int b=3; b=b >> 1; System.out.println(b); 它可以完美工作,但是当我将变量b更改为byte,short,float,double时,它包含错误,但是对于变量int和long来说,它可以完美工作,为什么它不能与其他变量一起工作? 参考方案 位移位运算符(例如>>)与任何整数类型兼…
通过Maven编译器插件不会发生有限的包含和排除 - java我正在使用3.6.0版的maven编译器插件,在此我们只想在特定文件夹中编译一个文件,而在该位置编译所有其他文件。例如:在文件夹应用程序中有14个文件,从那我只希望编译1个文件,但它编译了所有文件,如果我要排除,则它也不起作用。 <sourceDirectory>${basedir}/../src/java</sourceDirectory…