Validation - singhmahabir/allinone-rest-services GitHub Wiki
Welcome to the allinone-rest-services wiki!
@RequestMapping( params = { "term!=" } ) This param is used to validate request param should not be empty
JSR-303’ javax.validation.constraints package we have to use and its implementation classes is Hibernate validation
To Validate @RequestBody MyEndpointRequest class attributes you have 2 ways 1. @Valid and 2. @Validated • @Validated --> Use over class and it will work on pojo's attribute where validation annotations are used Ex validatePojo(@RequestBody MyEndpointRequest request) • @Valid --> If @Validated Is not available over class then use @Value on implemented class method Ex validatePojo(@Valid @RequestBody MyEndpointRequest request) you can use both without any problem
To validate @PathVariable and @RequestParam @Valid will not work instead @Validated will not work unless it is used At Class level still it will not work at method level , once @Validated is used in @RestController Then it will validate for all path ,request and body as well Note : To use this 1st @Validated used At Class level. 2nd Interface level and @Rest Class Level method argument should be same order except for RequestBody In Interface level if @Validated is not used for RequestBody and class level it is used then it will work But @Validated and @Valid combination should not be there
It will even work if @RequestParam is not used in classes and interface but validator annotations are used so it validate the parameters.
@NotNull is applied to integer and any Object type @Min and @Max will work to validate range of integer Ex : @NotNull(message = "The value of ts must not be empty") @Min(value = 1, message = "The value of ts must be grater than 0") private Integer ts;
@NotEmpty is applied to String and Collection because it contain @NotNull and @Size , @size is not work with integer. It is used to check size rather than contents and applies to Collections as well as Strings Note that @NotEmpty was added in version 4.1. For versions before 4.1, you will need to use a custom validator or @Pattern
@NotBlank is used with String for whether string value has space only or any value if only space then it failed to validate. which is specific to Strings and ignores trailing whitespace.
How To Trim the string value At first Spring will use setter-method to set value of property. And for validate value Spring will get it with getter-method. That means, you can trim value in setter-method for prepare it to validation: It will work for @PathVariable ,@RequestParam But not for @RequestBody Class
@ControllerAdvice public class ControllerConfig { @InitBinder public void initBinder ( WebDataBinder binder ) { StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true); binder.registerCustomEditor(String.class, stringtrimmer); } }
Another way to use Json public class WhiteSpaceRemovalDeserializer extends JsonDeserializer { @Override public String deserialize(JsonParser jp, DeserializationContext ctxt) { // This is where you can deserialize your value the way you want. // Don't know if the following expression is correct, this is just an idea. return jp.getCurrentToken().asText().trim(); } } and set this to your property @JsonDeserialize(using=WhiteSpaceRemovalSerializer.class) public void setAString(String aString) { // body }
@Value("${ass.maxOptimisticLockRetries:10}")
http://blog.codeleak.pl/2013/09/request-body-validation-in-spring-mvc-3.2.html