package com.ruoyi.handler;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.customize.CustomizeException;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 异常拦截Handler
*
*/
@RestControllerAdvice
public class BadRequestExceptionHandler {
/**
* 自定义异常
*
* @param e
* @return
*/
@ExceptionHandler(CustomizeException.class)
public ResponseEntity customizeExceptionHandler(CustomizeException e) {
return ResponseEntity.ok(AjaxResult.error(e.getCode(), e.getBusinessCode(), e.getMessage()));
}
/**
* 文件太大了
*
* @param e
* @return
*/
@ExceptionHandler(IllegalStateException.class)
public ResponseEntity fileSizeLimitExceededExceptionHandler(FileSizeLimitExceededException e) {
return ResponseEntity.ok(AjaxResult.error("文件太大了"));
}
}
使用@RestControllerAdvice注解实现全局异常拦截
于 2023-10-31 11:11:41 首次发布