springboot-全局捕获异常
拦截controller 层,抛出的异常,根据不同的异常类型,进行相应的处理。
package com.test.intercept;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* 全局捕获异常
*/
@ControllerAdvice
public class GFlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, Object> exceptionHandler(){
Map<String, Object> result = new HashMap<>();
result.put("code", "500");
result.put("msg", "系统错误,请稍后重试。。。");
return result;
}
}