springboot全局异常处理
@ControllerAdvice, 这里返回的json
package com.example.hander;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
// mvc的异常
@ControllerAdvice
// 如果是Rest就用RestControllerAdvice
public class ExceptionHandlerDemo {
/**
* 全局异常捕捉处理
*
* @Author: lxx
* @Date: 2020-05-18 16:19:54
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Map<String, Object> errorHandler(Exception e) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", 500);
map.put("msg", e.getMessage());
return map;
}
}