I am trying to implement Javax Validation on Immutable objects, but I only get it working by renaming every variable with prefix "get". It is possible to make it work without that "get" ?
@Value.Immutable
public interface Entity {
@Min(19)
int getAge();
@NotBlank
@Size(min = 10)
String getName();
}
my controller:
@PostMapping("/entity/")
public ResponseEntity post2(@RequestBody @Valid ImmutableEntity entity) {
return new ResponseEntity<Object>("", HttpStatus.OK);
}
If I understand
@Value.Immutablecorrectly, you are supposed to annotate it on a class or interface with the methods representing the to-be-generated fields. Example:This would mean that the following would be generated:
If you do not follow the
getXXXXX()pattern you will get a different generated class as follows:With this one you would get the following generated class:
Mind the differences. In the first one, you have normal getters, in the second one you have "getters" without the prefix
getin the method name. Javax Validation uses normal getters to get the data so that it can be validated. In the second generated class, you getters do not follow the usual naming convention, and thus Javax Validation does not work.