Java 如何在提交操作期间仅在服务端向请求类添加验证
我在我的项目中有一个要求,即仅在提交操作期间向我的请求类添加验证。在保存操作期间,不需要空字段验证。现在我知道了两种验证请求类的方法Java 如何在提交操作期间仅在服务端向请求类添加验证,java,performance,validation,Java,Performance,Validation,我在我的项目中有一个要求,即仅在提交操作期间向我的请求类添加验证。在保存操作期间,不需要空字段验证。现在我知道了两种验证请求类的方法 在请求类中为相应字段使用@NotBlank注释。但我不能使用这种方式,因为我的要求是特定于提交操作。对于save和submit,我使用相同的请求类 使用message.properties文件,我可以在其中定义错误并在我的验证器类中使用它们,例如: Validator.java private void validateIncidentDetails(Create
private void validateIncidentDetails(CreateIncidentRequest request) {
...
if(checkForNullOrEmpty(request.getDetectionDate())) {
String msg = messageBundleService.fetchMessage("ERR_EMPTY_DETECTION_DATE");
throw new BadRequestException(msg);
}
---
}
**每个领域都是如此
errorMessage.properties
ERR_EMPTY_DETECTION_DATE=Detection Date is required
现在我的问题是,有没有其他更好的方法来实现上述要求。您可以尝试这种方法来检查null
public boolean checkNull() throws IllegalAccessException {
for (Field f : getClass().getDeclaredFields())
f.setAccessible(true);//to access private property
if (f.get(this) != null)
return false;
return true;
}
您可以尝试这种方法来检查null
public boolean checkNull() throws IllegalAccessException {
for (Field f : getClass().getDeclaredFields())
f.setAccessible(true);//to access private property
if (f.get(this) != null)
return false;
return true;
}
反思是实现这一目标的最佳方式,检查我的答案。反思是实现这一目标的最佳方式检查我的答案。