add
This commit is contained in:
parent
ca3efd0073
commit
fd8fe53377
@ -3,11 +3,16 @@ package com.fanxb.exceptiontest.config;
|
|||||||
import com.fanxb.exceptiontest.entity.Result;
|
import com.fanxb.exceptiontest.entity.Result;
|
||||||
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.CustomValidException;
|
||||||
import com.fasterxml.jackson.databind.ser.Serializers;
|
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolationException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author fanxb
|
* @author fanxb
|
||||||
* @date 2021-09-24-下午4:37
|
* @date 2021-09-24-下午4:37
|
||||||
@ -26,6 +31,15 @@ public class ExceptionHandle {
|
|||||||
//可在这里进行一些针对特定异常的处理逻辑
|
//可在这里进行一些针对特定异常的处理逻辑
|
||||||
log.info("customBusinessException:{}", be.getMessage());
|
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 {
|
} else {
|
||||||
//其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息)
|
//其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息)
|
||||||
log.error("other exception:{}", e.getMessage(), e);
|
log.error("other exception:{}", e.getMessage(), e);
|
||||||
|
@ -5,18 +5,19 @@ import com.fanxb.exceptiontest.entity.consistant.Insert;
|
|||||||
import com.fanxb.exceptiontest.entity.consistant.Update;
|
import com.fanxb.exceptiontest.entity.consistant.Update;
|
||||||
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
import com.fanxb.exceptiontest.entity.vo.TestBody;
|
import com.fanxb.exceptiontest.entity.vo.*;
|
||||||
import com.fanxb.exceptiontest.entity.vo.TestBody2;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.fanxb.exceptiontest.entity.vo.TestBody3;
|
|
||||||
import com.fanxb.exceptiontest.entity.vo.TestBody4;
|
|
||||||
import org.hibernate.validator.constraints.Range;
|
import org.hibernate.validator.constraints.Range;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.Email;
|
import javax.validation.constraints.Email;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author fanxb
|
* @author fanxb
|
||||||
@ -24,6 +25,7 @@ import javax.validation.constraints.NotBlank;
|
|||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@Validated
|
@Validated
|
||||||
|
@Slf4j
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,8 +72,26 @@ public class TestController {
|
|||||||
* 自定义校验
|
* 自定义校验
|
||||||
*/
|
*/
|
||||||
@PostMapping("/test6")
|
@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;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user