首页 > 编程语言 > 详细

SpringBoot ControllerAdvice

时间:2019-02-24 21:50:30      阅读:196      评论:0      收藏:0      [点我收藏+]

在Spring3.2中新增了@ControllerAdvice注解,可用于定义@ExceptionHandler @ModelAttribute @InitBinder,并应用到所有被@RequestMapping注解的方法中。

示例,全局异常捕获处理:

  // 抛出Exception异常
    @GetMapping(value = "girls/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception{
        girlService.getAge(id);
    }

 // 编写@ControllerAdvice
@ControllerAdvice
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        return ResultUtil.error(100, e.getMessage());
    }
}

SpringBoot ControllerAdvice

原文:https://www.cnblogs.com/vincenshen/p/10427886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!