I'm using Hibernate Validator
annotations with individual parameters of my Spring @Controller
, as shown below:
@RequestMapping(value = URIConstants.SOME_URI, method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> tryValidation(
@Size(min = 1, max = 20)
@NotBlank @RequestParam(required = false) String something) {
return ResponseEntity.status(HttpStatus.OK).body(something);
}
As per my understanding, @NotBlank
and @NotEmpty
both use @NotNull
, but what if I want to use @NotBlank
without @NotNull
? Basically, I want the parameter to not be mandatory, but when it is defined it should not be null or empty. However, when I try to do this, the @RequestParam(required = false)
doesn't matter and you end up having to define the parameter.
I know I could write a custom validator but is there a way to do so within the existing functionality of Hibernate Validator
?