springboot简单的异常处理例子

本文介绍了SpringMVC中如何处理异常,包括@ControllerAdvice注解用于定义全局异常处理器,@ExceptionHandler在Controller级别和全局级别处理异常,以及BasicErrorController作为备用的异常处理器,处理未被其他处理器捕获的异常。此外,还提到了不同层次的异常处理策略。
摘要由CSDN通过智能技术生成

1. ExceptionHandler仅仅处理当前controller的Exception

@RestController

@RequestMapping("/ex")

public class ExController {

@GetMapping("thex")

public String throwEx() throws Exception {

    throw new Exception("exception thrown from ExController.throwEx");

}

@GetMapping("fnfex")

public String FileNotFound() throws Exception {

    throw new FileNotFoundException("FileNotFoundException thrown from ExController.throwEx");

}

@ExceptionHandler(FileNotFoundException.class)

public String exHandler(Exception e) {

  System.out

 .println("==================ExController.exHandler===================================================");

  e.printStackTrace();

  return e.getMessage() + " from ExController.exHandler";

}

}

2.@ControllerAdvice

用以定义全局异常处理,当controller自己的@ExceptionHandler无法处理时,就交给全局的@ControllerAdvice 中定义的@ExceptionHandler,

@ExceptionHandler methods in @ControllerAdvice can be used to handle exceptions from any @Controller or any other handler.

@ControllerAdvice(annotations = { Controller.class, RestController.class })

public class AdController {

@ResponseBody

@ExceptionHandler(Exception.class)

public Map errorHandler(Exception ex) {

Map errorMap = new HashMap();

errorMap.put("msg", ex.getMessage()+" from AdController");

System.out.println("---------------in AdController.errorHandler ");

return errorMap;

}

@ExceptionHandler(IOException.class)

public Map errorHandler2(IOException ex,, HttpServletRequest request) {

.....

}

@ExceptionHandler(FileNotFoundException.class)

public Map errorHandler3(FileNotFoundException ex) {

.....

}

}

3.BasicErrorController

BasicErrorController用来处理前两个处理不了的Exception

@Slf4j

@RestController

public class ExAdController extends BasicErrorController {

public ExAdController(ErrorProperties ep) {

super(new DefaultErrorAttributes(),ep);

}

@Override

@RequestMapping(value = "", produces = {MediaType.APPLICATION_JSON_VALUE})

public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {

// 获取错误信息

Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));

HttpStatus status = getStatus(request);

String code = body.get("status").toString();

String message = body.get("message").toString();

String trace="";

if(this.getErrorProperties().getIncludeStacktrace()==IncludeAttribute.ALWAYS) {

Object sb=body.get("trace");

trace=sb.toString();

}

return new ResponseEntity(message+" dealt by ExAdController \r\n "+trace , HttpStatus.OK);

}

}

在1中处理不了的Exception会在BasicErrorController 进行处理

-----------------------------------------------------------------------------------------------------------------------------------------------

@ControllerAdvice("org.example.controllers")
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
@ControllerAdvice(annotations = RestController.class)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值