在我的 Spring Rest Endpoint 中,我接收 JSON 作为包装在请求参数中的字符串。我可以使用 JSON 类的 ObjectMapper 将 JSON 字符串反序列化为对象。但是,我想验证对象的属性,即名字、姓氏是否为空或 null 以及电话号码是否为 10 位数字以及其他验证等等
我的问题是如何在 Spring Boot Rest 中实现对象的验证,而无需在 Controller 方法中使用 @Valid 注释
@PostMapping(value = "/saveEmployee")
public ResponseEntity<?> saveEmployeeDetails(
@Valid @RequestPart(value = "empData", required = true) String emplRegJSONString,
@RequestParam("file") MultipartFile uploadFile, BindingResult result) {
Status status = new Status();
try {
LOGGER.info("Request Body is " + emplRegJSONString);
Long savedEmployeeRegisId = null;
if (StringUtils.isNotBlank(emplRegJSONString)) {
EmployeeRegistrationTbl employeeRegistrationTbl = new ObjectMapper().readValue(emplRegJSONString,
EmployeeRegistrationTbl.class);
// VALIDATION SHOULD GO AHEAD HERE ON EmployeeRegistrationTbl object
}
}