Can anyone suggest a good validation framework we can use in our java application? There are no forms in our application but we pass retrieve and send data to multiple systems. Once we get this data, we want to transform it into a pojo and perform some validation on that pojo. A validation framework that allows you to put开发者_运维知识库 your validation rules into an xml or flat file would be ideal
Any help on this is greatly appreciated
Thanks Damien
Check out Hibernate Validator.
The default metadata source is annotations, with the ability to override and extend the meta-data through the use of XML validation descriptors.
Java EE 6 has a pretty powerful validation mechanism exactly through annotations (such as @NotNull, @Size, etc.). Take a look at Java Bean Validation. The best part is that you can define your own such annotations which would do any custom validation you might need.
There's also Oval where constraints can be declared with annotations (@NotNull, @MaxLength), POJOs or XML. You can also create your own custom constraints.
The Ujorm framework based on key-value domain objects offers special validators where the main features are:
- validator is invoked on value writting events always
- validators can be joined using AND / OR operators
- validator is assigned to the key optionally
Sample of the use validator in a JavaBean:
import static org.ujorm.Validator.Build.*;
public class ValidBo extends AbstractUjo implements Serializable {
/** Factory */
private static final KeyFactory<ValidBo> f = newFactory(ValidBo.class);
/** Keys: */
public static final Key<ValidBo, Long> PID = f.newKey(notNull());
public static final Key<ValidBo, Integer> CODE = f.newKey(between(0, 10));
public static final Key<ValidBo, String> NAME = f.newKey(regexp("T.*T"));
public static final Key<ValidBo, Double> CASH = f.newKey(min(0.0).and(notNull()));
static {
f.lock();
}
// Setters:
public void setPid(Long pid) {
PID.setValue(this, pid);
}
public void setCode(Integer code) {
CODE.setValue(this, code);
}
public void setName(String name) {
NAME.setValue(this, name);
}
public void setCash(Double cash) {
CASH.setValue(this, cash);
}
// Getters ...
}
See the documentation for more information.
精彩评论