I am to validate an incoming request body for field validation errors but there are no errors caught in the BindingResult.
I have tried using the @Valid annotations along with contraints annotations in the DTO but it isn't working.
Here is my controller class
@Slf4j
@RestController
@RequestMapping("/admin/staff")
@CrossOrigin("*")
public class AdminController {
@Autowired
private AdminService adminService;
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN')")
@PutMapping("/{id}")
public ResponseEntity<GenericResponse<UserDetailsDTO>> updateUser(
@PathVariable("id") String userId, @Valid @RequestBody
GenericRequest<StaffUpdateRequestDTO> request, BindingResult bindingResult
) {
exceptionDTO.setMessageId(request.getHeader().getMsgId());
if(bindingResult.hasErrors()){
throw ValidationError.builder().bindingResult(bindingResult).build();
}
if(!userId.equalsIgnoreCase(request.getTxnRequest().getUserDetails().getUserId())){
throw ValidationError.builder().bindingResult(bindingResult).build();
}
GenericResponse<UserDetailsDTO> response = new GenericResponse<>();
response.setHeader(adminUtil.createResponseHeader());
response.getHeader().setMsgId(request.getHeader().getMsgId());
response.getHeader().setChannelId(request.getHeader().getChannelId());
response.setTxnResponse(adminService.saveUser(request.getTxnRequest()));
return ResponseEntity.status(HttpStatus.OK).body(response);
}
}
Here are the DTOs
@Data
public class GenericRequest<T> {
@NotNull(message = "Validation failure")
private HeaderDTO header;
@NotNull(message = "Validation failure")
private T txnRequest;
}
@Data
public class HeaderDTO {
@NotNull
@NotEmpty
private String msgId;
@NotNull
@NotEmpty
private String timestamp;
@NotNull
@NotEmpty
private String channelId;
}
@Data
public class StaffUpdateRequestDTO {
@NotNull(message = "Validation failure")
private UserDetailsDTO userDetails;
@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation Failure")
private String approvedBy;
}
@Data
public class UserDetailsDTO {
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String userId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String userName;
@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation failure")
private String emailId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String countryCode;
@NotNull(message = "Validation failure")
private List<RoleDetailsDTO> roles;
}
@Data
public class RoleDetailsDTO {
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String roleId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String roleName;
}
Even if I do not send some fields from the UserDetailsDTO, the BindingResult error check fails in the below line :
if(bindingResult.hasErrors()){
throw ValidationError.builder().bindingResult(bindingResult).build();
}
example of request body sent :
{
"header":{
"msgId":"87612983612",
"timestamp":"2019-08-05T12:12:12.122",
"channelId":"BDSHK"
},
"txnRequest":{
"userDetails":{
"userId": "deepakkumarc",
"userName": "Deepak Kumar Choudhary",
"roles": [
{
"roleId": "SUPER_ADMIN",
"roleName": "SUPER ADMIN"
},
{
"roleId":"LI_PRODUCT_ADMIN",
"roleName":"LIFE INSURANCE PRODUCT ADMIN"
},
{
"roleId":"GI_PRODUCT_ADMIN",
"roleName":"GENERAL INSURANCE PRODUCT ADMIN"
}
]
},
"approvedBy":"deepakkumarc"
}
}
As seen above I am not sending emailId and countryCode but this passes validation.Expectation is to get validation errors if any of the validations fail.
How do I implement validation for this request body ?
Finally I figured out what was wrong. We need to put @Valid on the field level as well like below :
@Data
public class StaffUpdateRequestDTO {
@NotNull(message = "Validation failure")
@Valid
private UserDetailsDTO userDetails;
@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation Failure")
private String approvedBy;
}