Given below is my main controller from which I am calling the getPDFDetails method.
@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@RequestBody final UpdatePrintContracts updatePrintContracts) throws Exception {
System.out.println("contracts value is "+ updatePrintContracts);
Integer cancellationReasons = service.getPDFDetails(updatePrintContracts);
System.out.println("success!");
return ResponseEntity.ok(cancellationReasons);
}
Below is the UpdatePrintContracts class where I have defined all the variables with validation annotations and corresponding getter/setter methods.
public class UpdatePrintContracts {
@Valid
@NotBlank
@Pattern(regexp = "\\p{Alnum}{1,30}")
String isReprint;
@Valid
@NotBlank
Integer dealerId;
@Valid
@NotBlank
@Pattern(regexp = "\\p{Alnum}{1,30}")
String includeSignatureCoordinates;
@Valid
@NotBlank
java.util.List<Integer> contractNumbers;
public String getIsReprint() {
return isReprint;
}
public void setIsReprint(String isReprint) {
this.isReprint = isReprint;
}
public Integer getDealerId() {
return dealerId;
}
public void setDealerId(Integer dealerId) {
this.dealerId = dealerId;
}
public String getIncludeSignatureCoordinates() {
return includeSignatureCoordinates;
}
public void setIncludeSignatureCoordinates(String includeSignatureCoordinates) {
this.includeSignatureCoordinates = includeSignatureCoordinates;
}
public java.util.List<Integer> getContractNumbers() {
return contractNumbers;
}
public void setContractNumbers(java.util.List<Integer> contractNumbers) {
this.contractNumbers = contractNumbers;
}
}
I am trying to run the application as a Spring Boot app by right clicking on the project (Run As) and passing blank values for variables isReprint and includeSignatureCoordinates through Soap UI. However the validation doesn't seem to work and is not throwing any validation error in Soap UI. What am I missing? Any help is appreciated!
First you dont need to have @Valid annotation for those class variables in UpdatePrintContracts . You can delete them.
To trigger validation of a @Controller input, simply annotate the input argument as @Valid or @Validated:
@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@Valid @RequestBody final UpdatePrintContracts updatePrintContracts) throws Exception {
Refer here for full understanding of validating models in spring boot.
And If you want to check that a string contains only specific characters, you must add anchors (^ for beginning of the string, $ for end of the string) to be sure that your pattern matches all the string.Curly brackets are only to write a quantity,
@Pattern(regexp = "^[\\p{Alnum}]{1,32}$")
Lastly i assume you have following jars in your classpath,
.validation-api.jar (contains the abstract API and the annotation scanner)
.hibernate-validator.jar (contains the concrete implementation)