現在比較常用的方式是給前端返回JSON,但是有些時候程序出現一些異常,導致前端頁面報錯不是很友好,所以就需要對項目進行全局的異常捕獲,返回給前端固定的格式,進行友好的處理!
話不多說,開始寫代碼
一.全局異常捕獲
1.1 全局捕獲
@Slf4j
@RestControllerAdvice(annotations = {RestController.class, Service.class})
public class BaseExceptionHandler {
/**
* <p>
* 全局Base異常處理
* </p>
* @version 1.0.0
* @author Dylan
* @since 2020/2/27
* @param e
*/
@ExceptionHandler({BaseException.class,BusinessException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R customExceptionHandler(BaseException e) {
sysLog();
log.info("自定義異常 ---> {}",e);
return R.error(e.getCode(),e.getMsg());
}
/**
* <p>
* 其他類型的異常處理
* </p>
* @version 1.0.0
* @author Dylan
* @since 2020/2/27
* @param e
*/
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R customExceptionHandler(Exception e) {
sysLog();
log.info("未知的運行異常 ---> ",e);
return R.error();
}
/**
* <p>
* 參數異常處理
* </p>
* @version 1.0.0
* @author Dylan-haiji
* @since 2020/2/28
* @param exception
*/
@ExceptionHandler(value= MethodArgumentNotValidException.class)
public R methodNotValidHandler(MethodArgumentNotValidException exception) {
sysLog();
log.info("參數異常 ---> ",exception);
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
return R.error(fieldErrors.get(0).getDefaultMessage());
}
/**
* <p>
* 統一日誌輸出
* </p>
* @version 1.0.0
* @author Dylan-haiji
* @since 2020/2/28
* @param
* @return void
*/
private void sysLog(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestUri = request.getRequestURI();
log.info("異常 method ---> {}",request.getMethod());
log.info("異常 requestURI ---> {}",requestUri);
}
}
1.2 基礎異常
public class BaseException extends RuntimeException{
private Integer code;
private String msg;
public BaseException(Integer code, String msg){
this.code = code;
this.msg = msg;
}
public BaseException( String msg){
this.code = 500;
this.msg = msg;
}
}
1.3 業務參數異常
public class BusinessException extends BaseException {
public BusinessException(Integer code, String msg) {
super(code, msg);
}
public BusinessException() {
super(ConstantUtils.BUSINESS_DATA);
}
public BusinessException(String msg) {
super(msg);
}
}
二.驗證
通過上面的配置,我們可以進行全局的異常捕獲,接下來我們以參數驗證爲例進行驗證是否可用!
2.1 定義實體類
public class Demo implements Serializable {
@NotBlank(message = "請選擇時間")
private String endDate;
@NotBlank(message = "請選擇範圍類型")
private String value;
}
2.2 定義接口
@PostMapping(value = "list")
public R getOverView(@RequestBody @Valid Demo demo){
return R.ok();
}
這是我們調用接口,直傳一個參數!達到了我們的預期效果
當然我們處理業務的時候也可進行異常的拋出,這樣也會進行全局的捕獲!
這樣的接口返回就非常的友好了!
本文的分享暫時就到這裏,希望對您有所幫助
關注 Java有貨領取更多資料
聯繫小編。微信:372787553,帶您進羣互相學習
左側小編微信,右側獲取免費資料
- Java 設計模式學習代碼 https://github.com/Dylan-haiji/design-pattern
- SpringCloud學習代碼: https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud學習代碼:https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot+Mybatis 多數據源切換:https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redis、Mongo、Rabbitmq、Kafka學習代碼: https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot+SpringSecurity實現自定義登錄學習代碼:https://github.com/Dylan-haiji/javayh-distribution