Below is the mapped method that I am having trouble with, no matter what value I pass to it the validation returns "passed validation."
@RequestMapping(value = "test", method = RequestMethod.POST)
@ResponseBody
public String getTest(@RequestBody @Valid @Max(32) long longValue, BindingResult result) {
if (result.hasErrors()) {
return "failed validation";
} else {
return "passed validation";
}
}
I know the @Max works with my application because I use it for validation on custom objects that return a lot of data to the controller. It only doesn't run the validation in this case where I call @Valid and the type of validation on the object in the method parameters.
Is this not allowed with hibernate-validator?
I am hoping to not have to define an object that only contains a long value just so that I can run validation on it.
The Spring MVC framework provides us with standard predefined validators to validate user input data in a simple and straightforward way. The Bean Validation API is the popular approach for data validations in Spring applications.
So, for that, it uses Hibernate Validator. The Hibernate Validator is a fully compliant JSR-303/309 implementation that allows to express and validate application constraints.
The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.
I am hoping to not have to define an object that only contains a long value just so that I can run validation on it.
Defining a wrapping bean would IMHO be the smartest move, as hibernate-validator is completly centered around the notion of the bean, and is, after all, a reference implementation of the bean validation specification. One of the primary motivators of the spec is to acknowledge validation as a cross-cutting concern that spans across different app layers, and provide a mechanism to gracefully handle this. That is the reason why it is centered around beans, its the objects that get passed through the layers.
On the other hand, validating primitves programtically is not a big deal after all, your code can simply be something like
@RequestMapping(value = "test", method = RequestMethod.POST)
@ResponseBody
public String getTest(@RequestBody long longValue, BindingResult result) {
if (longValue > 32) {
result.rejectValue("longValue", "error.longValue", "longValue max constrained failed");
return "failed validation";
} else {
return "passed validation";
}
}
So in my opinion, either go for the programatic validation if its simple enough, or simply wrap the value.
No, it is not allowed. I see your point however the JSR-303 specification (which hibernate validator implements) are meant for beans validation, see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With