@ControllerAdvice
public class AdviceController {
private Logger logger = LogManager.getLogger();
/** 业务异常 */
private static final int FAIL_STATUS = 400;
/** 异常 */
private static final int ERROR_STATUS = 500;
/** 异常处理 */
@ExceptionHandler(Exception.class)
@ResponseBody
public ObjectRestResponse exceptionHandler(HttpServletRequest request, HttpServletResponse response,
Exception ex) throws Exception {
ObjectRestResponse restResponse = new ObjectRestResponse();
System.out.println("捕获异常:" + ex.getLocalizedMessage());
logger.error("捕获异常:" + ex.getLocalizedMessage());
if (ex instanceof RuntimeException) {
restResponse.setStatus(FAIL_STATUS);
restResponse.setMessage(ex.getMessage());
}
else {
restResponse.setStatus(ERROR_STATUS);
restResponse.setMessage(ex.getMessage());
}
return restResponse;
}
}
spring @ControllerAdvice处理异常
最新推荐文章于 2022-09-17 14:31:20 发布