This commit is contained in:
fanxb 2021-10-09 16:31:04 +08:00
parent ca3efd0073
commit fd8fe53377
4 changed files with 74 additions and 5 deletions

View File

@ -3,11 +3,16 @@ package com.fanxb.exceptiontest.config;
import com.fanxb.exceptiontest.entity.Result;
import com.fanxb.exceptiontest.entity.exception.BaseException;
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
import com.fanxb.exceptiontest.entity.exception.CustomValidException;
import com.fasterxml.jackson.databind.ser.Serializers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
import java.util.Objects;
/**
* @author fanxb
* @date 2021-09-24-下午4:37
@ -26,6 +31,15 @@ public class ExceptionHandle {
//可在这里进行一些针对特定异常的处理逻辑
log.info("customBusinessException:{}", be.getMessage());
}
} else if (e instanceof ConstraintViolationException) {
//url参数数组类参数校验报错类
ConstraintViolationException ce = (ConstraintViolationException) e;
//针对参数校验异常建立了一个异常类
be = new CustomValidException(ce.getMessage());
} else if (e instanceof MethodArgumentNotValidException) {
//json对象类参数校验报错类
MethodArgumentNotValidException ce = (MethodArgumentNotValidException) e;
be = new CustomValidException(Objects.requireNonNull(ce.getFieldError()).getDefaultMessage());
} else {
//其它异常非自动抛出的,无需给前端返回具体错误内容用户不需要看见空指针之类的异常信息
log.error("other exception:{}", e.getMessage(), e);

View File

@ -5,18 +5,19 @@ import com.fanxb.exceptiontest.entity.consistant.Insert;
import com.fanxb.exceptiontest.entity.consistant.Update;
import com.fanxb.exceptiontest.entity.exception.BaseException;
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
import com.fanxb.exceptiontest.entity.vo.TestBody;
import com.fanxb.exceptiontest.entity.vo.TestBody2;
import com.fanxb.exceptiontest.entity.vo.TestBody3;
import com.fanxb.exceptiontest.entity.vo.TestBody4;
import com.fanxb.exceptiontest.entity.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;
/**
* @author fanxb
@ -24,6 +25,7 @@ import javax.validation.constraints.NotBlank;
*/
@RestController
@Validated
@Slf4j
public class TestController {
/**
@ -70,8 +72,26 @@ public class TestController {
* 自定义校验
*/
@PostMapping("/test6")
public Result test6(@Validated @RequestBody TestBody4 testBody) {
public Result test6(@Validated @RequestBody TestBody4 testBody,BindingResult result) {
if(result.hasErrors()){
log.info("asdf");
}
return null;
}
/**
* 集合校验1(对象内对象集合)
*/
@PostMapping("/test7")
public Result test7(@Validated @RequestBody TestBody5 testBody) {
return null;
}
/**
* 集合校验2对象集合
*/
@PostMapping("/test8")
public Result test8(@Validated @RequestBody List<@Valid TestBody4> list) {
return null;
}
}

View File

@ -0,0 +1,18 @@
package com.fanxb.exceptiontest.entity.exception;
/**
* @author fanxb
* @date 2021-10-09-下午4:06
*/
public class CustomValidException extends BaseException {
private static final long serialVersionUID = -5540634625578625266L;
/**
* 自定义业务异常错误码
*/
private static final int ERROR_CODE = -2;
public CustomValidException(String message) {
super(message, ERROR_CODE, null);
}
}

View File

@ -0,0 +1,17 @@
package com.fanxb.exceptiontest.entity.vo;
import com.fanxb.exceptiontest.entity.validation.annotation.CustomCheck;
import lombok.Data;
import javax.validation.Valid;
import java.util.List;
/**
* @author fanxb
* @date 2021-09-28-下午4:29
*/
@Data
public class TestBody5 {
@Valid
private List<TestBody4> list;
}