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(basePackages = "com.example.controller")
public class globalExceptionHandler {
//json格式
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String,Object> errorResult(RuntimeException re){
System.out.println(re.getMessage());
Map<String,Object> errorResultMap = new HashMap<String,Object>();
errorResultMap.put("errorCode","500");
errorResultMap.put("errorMsg","全局捕获异常:系统错误!");
return errorResultMap;
}
//jsp格式
@ExceptionHandler(RuntimeException.class)
public String errorResult(RuntimeException re) {
System.out.println(re.getMessage());
Map<String, Object> errorResultMap = new HashMap<String, Object>();
errorResultMap.put("errorCode", "500");
errorResultMap.put("errorMsg", "全局捕获异常:系统错误!");
return "error";
}
}