gpt4 book ai didi

java - @NotNull : validation custom message not displaying

转载 作者:行者123 更新时间:2023-11-30 02:02:27 33 4
gpt4 key购买 nike

我正在使用 Spring Data JPA 来创建应用程序。我正在尝试使用注释来实现服务器端验证。我在带有自定义消息的字段中添加了 @NotNull 注释。我还添加了 @valid@RequestBody

但问题是,当我将 nAccountId 作为 null 传递时,我没有收到自定义消息,即 Account id can not be null 我是获取“message”:“object='accountMaintenanceSave'验证失败。错误计数:1”,

谁能告诉我为什么我没有收到自定义消息?

Controller 代码

@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);
}
}

我不知道之前到底发生了什么,也没有收到正确的消息。现在使用相同的代码得到这样的结果和正确的消息

{
"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]吗?

最佳答案

试试这个。

@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);
}
}

关于java - @NotNull : validation custom message not displaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52386294/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com