新建GlobalExceptionHandler类。
package com.java1234.exception;import com.java1234.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;/*** 全局异常处理* @author java1234_小锋 (公众号:java1234)* @site www.java1234.vip* @company 南通小锋网络科技有限公司*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(value = RuntimeException.class)public R handler(RuntimeException e){log.error("运行时异常:----------------{}", e.getMessage());System.out.println("运行时异常:");return R.error(e.getMessage());}}
异常的返回结果更友好,适合前端分离项目;