Java 如何为requestbody发送自定义验证Json对象?
如何发送自定义JSON错误而不是spring提供的错误?我需要自定义验证和默认验证,比如Java 如何为requestbody发送自定义验证Json对象?,java,spring-boot,Java,Spring Boot,如何发送自定义JSON错误而不是spring提供的错误?我需要自定义验证和默认验证,比如@NotNull、@NotEmpty、@Email等等 仅当未违反POJO类级别中的验证约束时,才会触发该函数 代码 创建如下pojo: public class ValidationError { private String name; private String msg;* 并创建另一个pojo: public class ValidationErrorResponse {
@NotNull、@NotEmpty、@Email
等等
仅当未违反
POJO
类级别中的验证约束时,才会触发该函数
代码
创建如下pojo:
public class ValidationError {
private String name;
private String msg;*
并创建另一个pojo:
public class ValidationErrorResponse {
private List<ValidationError> errors;
公共类验证错误响应{
私有列表错误;
添加此方法:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ValidationErrorResponse handleValidationException(
BindingResult exception) {
List<ValidationError> errors = new ArrayList<ValidationError>();
for (ObjectError er : exception.getAllErrors()) {
FieldError fl = (FieldError) er;
errors.add(new ValidationError(fl.getField(), er
.getDefaultMessage()));
}
return new ValidationErrorResponse(errors);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@应答器
公共验证错误响应handleValidationException(
BindingResult(异常){
列表错误=新建ArrayList();
for(ObjectError:exception.getAllErrors()){
FieldError fl=(FieldError)er;
errors.add(新的ValidationError(fl.getField(),er
.getDefaultMessage());
}
返回新的ValidationErrorResponse(错误);
}
并更改您的方法:
@RequestMapping(value = "/v1/user", method = RequestMethod.POST)
public ResponseEntity<?>registerUser(@RequestBody @Valid UserRegistrationInfo userRegistrationInfo, HttpServletRequest request,
BindingResult bindingResult) {
if(bindingResult.hasErrors()){
return new ResponseEntity<ValidationErrorResponse>(
handleValidationException(br), HttpStatus.BAD_REQUEST);
}
// your bussines logic here
return new ResponseEntity ....
}
@RequestMapping(value=“/v1/user”,method=RequestMethod.POST)
公共响应EntityRegisterUser(@RequestBody@Valid UserRegistrationInfo UserRegistrationInfo,HttpServletRequest,
BindingResult(绑定结果){
if(bindingResult.hasErrors()){
返回新响应(
handleValidationException(br),HttpStatus.BAD_请求);
}
//你的业务逻辑在这里
返回新的响应。。。。
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_请求)
@应答器
公共验证错误响应句柄AthoDargumentNotValidException(
方法ArgumentNotValidException方法ArgumentNotValidException){
BindingResult BindingResult=methodArgumentNotValidException.getBindingResult();
List validationErrors=new ArrayList();
if(bindingResult.hasErrors()){
List fieldErrors=bindingResult.getFieldErrors();
fieldErrors.stream().forEach(fieldError->{
添加(新的ValidationError(fieldError.getField(),fieldError.getDefaultMessage());
});
}
返回新的ValidationErrorResponse(validationErrors);
}
只有在POJO类级别中的验证约束未被违反且不起作用的情况下,才会触发该函数。最后,我得到了解决方案。异常处理程序可以使用MethodArgumentNotValidException参数化,并从异常对象bcoz获取绑定结果,如果是MethodArgumentNotValidExcepe,则在API之前调用该异常对象异常处理程序可以用MethodArgumentNotValidException参数化,并从异常对象bcoz获取绑定结果。在MethodArgumentNotValidException的情况下,它在API之前被调用
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ValidationErrorResponse handleValidationException(
BindingResult exception) {
List<ValidationError> errors = new ArrayList<ValidationError>();
for (ObjectError er : exception.getAllErrors()) {
FieldError fl = (FieldError) er;
errors.add(new ValidationError(fl.getField(), er
.getDefaultMessage()));
}
return new ValidationErrorResponse(errors);
}
@RequestMapping(value = "/v1/user", method = RequestMethod.POST)
public ResponseEntity<?>registerUser(@RequestBody @Valid UserRegistrationInfo userRegistrationInfo, HttpServletRequest request,
BindingResult bindingResult) {
if(bindingResult.hasErrors()){
return new ResponseEntity<ValidationErrorResponse>(
handleValidationException(br), HttpStatus.BAD_REQUEST);
}
// your bussines logic here
return new ResponseEntity ....
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationErrorResponse handleMathodArgumentNotValidException(
MethodArgumentNotValidException methodArgumentNotValidException) {
BindingResult bindingResult = methodArgumentNotValidException.getBindingResult();
List<ValidationError> validationErrors = new ArrayList<ValidationError>();
if (bindingResult.hasErrors()) {
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
fieldErrors.stream().forEach(fieldError -> {
validationErrors.add(new ValidationError(fieldError.getField(), fieldError.getDefaultMessage()));
});
}
return new ValidationErrorResponse(validationErrors);
}