问题描述
我正在使用 spring 数据 JPA 来创建应用程序.因为我正在尝试使用注释来实现服务器端验证.我在带有自定义消息的文件中添加了 @NotNull
注释.我还添加了 @valid
和 @RequestBody
I am using spring data JPA for creating application. In that I am trying to implement server side validation using annotation. I added @NotNull
annotation on filed with custom message. I also added @valid
with @RequestBody
但问题是,当我将 nAccountId
作为 null
传递时,我没有收到自定义消息,即 Account id can not be null
我是获取消息":object='accountMaintenanceSave' 的验证失败.错误计数:1",
.
But problem is that when I am passing nAccountId
as null
I am not getting custom message i.e. Account id can not be null
I am getting "message": "Validation failed for object='accountMaintenanceSave'. Error count: 1",
.
谁能告诉我为什么我没有收到自定义消息?
Can any one please tell me why I am not getting custom message?
控制器代码
@PutMapping("/updateAccountData")
public ResponseEntity<Object> saveData(@Valid @RequestBody AccountMaintenanceSave saveObj){
return accService.saveData(saveObj);
}
AccountMaintenanceSave 类
import javax.validation.constraints.NotNull;
public class AccountMaintenanceSave {
@NotNull(message="Account id can not be null")
public Integer nAccountId;
@NotNull
public String sClientAcctId;
@NotNull
public String sAcctDesc;
@NotNull
public String sLocation;
@NotNull
public Integer nDeptId;
@NotNull
public Integer nAccountCPCMappingid;
@NotNull
public Integer nInvestigatorId;
//Getter and Setter
}
RestExceptionHandler 类
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleAllExceptionMethod(Exception ex, WebRequest requset) {
ExceptionMessage exceptionMessageObj = new ExceptionMessage();
exceptionMessageObj.setMessage(ex.getLocalizedMessage());
exceptionMessageObj.setError(ex.getClass().getCanonicalName());
exceptionMessageObj.setPath(((ServletWebRequest) requset).getRequest().getServletPath());
// return exceptionMessageObj;
return new ResponseEntity<>(exceptionMessageObj, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我不知道之前到底发生了什么,也没有收到正确的消息.现在使用相同的代码得到这样的结果,并带有正确的消息
I don't know what exactly happen previously and not getting proper message. Now using same code getting result like this with proper message
{
"message": "Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity<java.lang.Object> com.spacestudy.controller.AccountController.saveData(com.spacestudy.model.AccountMaintenanceSave), with 1 error(s): [Field error in object 'accountMaintenanceSave' on field 'nAccountId': rejected value [null]; codes [NotNull.accountMaintenanceSave.nAccountId,NotNull.nAccountId,NotNull.java.lang.Integer,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [accountMaintenanceSave.nAccountId,nAccountId]; arguments []; default message [nAccountId]]; default message [Account id can not be null]] ",
"error": "org.springframework.web.bind.MethodArgumentNotValidException",
"path": "/spacestudy/rockefeller/admin/account/updateAccountData"
}
在message
中,我可以只打印[Account id can not be null]
吗?
In message
filed can I print only [Account id can not be null]
?
推荐答案
试试这个.
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleAllExceptionMethod(Exception ex, WebRequest requset) {
ExceptionMessage exceptionMessageObj = new ExceptionMessage();
// Handle All Field Validation Errors
if(ex instanceof MethodArgumentNotValidException) {
StringBuilder sb = new StringBuilder();
List<FieldError> fieldErrors = ((MethodArgumentNotValidException) ex).getBindingResult().getFieldErrors();
for(FieldError fieldError: fieldErrors){
sb.append(fieldError.getDefaultMessage());
sb.append(";");
}
exceptionMessageObj.setMessage(sb.toString());
}else{
exceptionMessageObj.setMessage(ex.getLocalizedMessage());
}
exceptionMessageObj.setError(ex.getClass().getCanonicalName());
exceptionMessageObj.setPath(((ServletWebRequest) requset).getRequest().getServletPath());
// return exceptionMessageObj;
return new ResponseEntity<>(exceptionMessageObj, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
这篇关于@NotNull :验证自定义消息不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!