springBoot入门总结(九)全局异常捕获

spring3.2中新增了 @ControllerAdvice (等同于 @RestControllerAdvice + @ResponseBody 注解)
@ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类。
@ControllerAdvice 可以指定扫描范围。
@ControllerAdvice 约定了几种可行的返回类型。返回 String,表示跳到某个 view;返回 modelAndView;如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换。
组合 @ExceptionHandler、@ModelAttribute 应用于被 @RequestMapping 注解的方法之上。

一、@RestControllerAdvice + @ExceptionHandler 捕获异常

package com.demo.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

    /**
     * 捕获全局异常
     * */
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> defaultExceptionHandler(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("status",500);
        map.put("message","系统异常");
        return map;
    }
}
package com.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/error")
public class ErrorController {

    //http://localhost:8080/error/test
    @RequestMapping("/test")
    public String test(){
        int i = 1/0;//抛出异常
        return "success";
    }
}

访问:http://localhost:8080/error/test

结果:

二、@ControllerAdvice + @ModelAttribute 把值绑定到Model中,使全局@RequestMapping可以获取到该值

package com.demo.handler;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

    /**
     * 把值绑定到Model中,使全局@RequestMapping可以获取到该值
     * */
    @ModelAttribute
    public void addAttributes(Model model){
        model.addAttribute("code","1010");
    }
}

 

package com.demo.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * http://localhost:8080/test/addAttribute
     *
     * 通过 @ModelAttribute 获取 @ControllerAdvice + @ModelAttribute 设置的值
     * */
    @RequestMapping("/addAttribute")
    public String addAttribute(@ModelAttribute("code") String code){
        return code;
    }
}

访问:http://localhost:8080/test/addAttribute

结果:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,可以通过自定义全局异常处理捕获和处理应用程序中的异常。以下是一种常见的实现方式: 首先,创建一个全局异常处理类,例如 `GlobalExceptionHandler`,并使用 `@ControllerAdvice` 注解将其标记为全局异常处理。在类中可以定义多个方法来处理不同类型的异常。 ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { // 处理异常逻辑 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); } @ExceptionHandler(MyCustomException.class) public ResponseEntity<String> handleMyCustomException(MyCustomException ex) { // 处理自定义异常逻辑 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); } // 其他异常处理方法... } ``` 在以上代码中,`@ExceptionHandler` 注解用于指定每个方法要处理的异常类型。在方法中,你可以根据具体需求编写异常处理逻辑,并返回合适的响应。 需要注意的是,全局异常处理类需要被扫描到,可以通过将其所在的包作为扫描路径配置到 Spring Boot 的配置文件中,或者使用 `@ComponentScan` 注解进行手动配置。 另外,还可以通过实现 `ErrorController` 接口来处理 Spring Boot 应用程序中无法被 `GlobalExceptionHandler` 捕获的错误。 这样,当应用程序中发生异常时,全局异常处理捕获并处理它们,返回相应的错误信息给客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值