Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC with hibernate Validator to validate single basic type

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.

like image 589
adtrano Avatar asked Jul 15 '14 20:07

adtrano


People also ask

Does Spring MVC provide validation support?

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.

Which is used to start the Validator in Spring MVC validation?

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.

What is the use of @valid annotation in Spring MVC?

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.


2 Answers

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.

like image 170
Master Slave Avatar answered Sep 17 '22 12:09

Master Slave


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

like image 22
Paizo Avatar answered Sep 21 '22 12:09

Paizo