zoukankan      html  css  js  c++  java
  • springboot全局异常处理

      项目中遇到运行时异常,总不能每个异常处理都去添加try catch逻辑,甚至有的时候一个偶然条件发生而导致异常,而我们没有进行对应的处理则会直接给请求者返回未知的错误,这在正式的上线的项目中是不允许,所以我们来配置全局异常处理。

      1、使用到的注解:@ControllerAdvice注解是用来配置控制器通知的,我们可以配置过滤拦截具体一种或者多种类型的注解,添加annotations属性即可,在类的上方我们配置了@ControllerAdvice的annotations属性值为RestController.class,也就是只有添加了@RestController注解的控制器才会进入全局异常处理;因为我们全局返回的都是统一的Json格式的字符串,所以需要再类上配置@ResponseBody注解;@ExceptionHandler注解用来配置需要拦截的异常类型,默认是全局类型,可以通过value属性配置只对某个类型的异常起作用;@ResponseStatus注解用于配置遇到该异常后返回数据时的StatusCode的值,我们这里默认使用值500;

      RestExceptionHandler配置代码如下

    package com.example.demo;
    
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.RestController;
    
    @ControllerAdvice(annotations=RestController.class)
    @ResponseBody
    public class RestExceptionHandler {
    
    	@ExceptionHandler
    //	@ExceptionHandler(value= {NumberFormatException.class}),只会在发生NumberFormatException时起作用
    	@ResponseStatus
    	public ResponseBean exceptionHandler(Exception e) {
    		ResponseBean rb = new ResponseBean();
    		rb.setStatus(false);
    		rb.setMsg(e.getMessage());
    		return rb;
    	}
    }
    

      2、响应实体类

    package com.example.demo;
    
    public class ResponseBean {
    	private boolean status;
    	private String msg;
    	private Object data;
    	public boolean getStatus() {
    		return status;
    	}
    	public void setStatus(boolean status) {
    		this.status = status;
    	}
    	public String getMsg() {
    		return msg;
    	}
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    	public Object getData() {
    		return data;
    	}
    	public void setData(Object data) {
    		this.data = data;
    	}
    }
    

      3、测试控制器类

    package com.example.demo;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class IndexController {
    
    	@GetMapping("/index")
    	public Object index() {
    		ResponseBean rb = new ResponseBean();
    		rb.setStatus(true);
    		rb.setMsg("response success");
    		//从service层获取到数据
    		rb.setData("正常响应的数据");
    		return rb;
    	}
    	@GetMapping("/secend")
    	public Object secend(int number) {
    		if(number ==1) {
    			throw new ArrayIndexOutOfBoundsException("数组下标越界");
    		}
    		ResponseBean rb = new ResponseBean();
    		rb.setStatus(true);
    		rb.setMsg("response success");
    		//从service层获取到数据
    		rb.setData("正常响应的数据");
    		return rb;
    	}
    	@GetMapping("/third")
    	public Object third(int number) {
    		if(number ==1) {
    			throw new NumberFormatException("转化异常");
    		}
    		ResponseBean rb = new ResponseBean();
    		rb.setStatus(true);
    		rb.setMsg("response success");
    		
    		rb.setData("正常响应的数据");
    		//从service层获取到数据,抛出
    		return rb;
    	}
    	@GetMapping("/four")
    	public Object four(int number) throws ClassNotFoundException {
    		if(number ==1) {
    			throw new ClassNotFoundException ("类找不到异常");
    		}
    		ResponseBean rb = new ResponseBean();
    		rb.setStatus(true);
    		rb.setMsg("response success");
    		
    		rb.setData("正常响应的数据");
    		//从service层获取到数据,抛出
    		return rb;
    	}
    }
    

      4、pom文件引入web启动包即可,启动类SpringbootExceptionApplication默认即可。

      启动项目,访问http://localhost:8080/index,得到正常的响应   {"status":true,"msg":"response success","data":"正常响应的数据"};访问http://localhost:8080/secend?number=1,得到异常处理的响应{"status":false,"msg":"数组下标越界","data":null};其它示例也都是预想中结果。

  • 相关阅读:
    UVa 1252 20个问题
    HDU 2196 Computer
    HDU 1520 Anniversary party
    HDU 2066 一个人的旅行
    UVa 10048 噪音恐惧症(Floyd)
    UVa 247 电话圈(Floyd传递闭包)
    HDU 2544 最短路(Dijkstra)
    HDU 1548 A strange lift (Dijkstra)
    UVa 1151 买还是建
    UVa 1395 苗条的生成树(Kruskal+并查集)
  • 原文地址:https://www.cnblogs.com/hhhshct/p/9250724.html
Copyright © 2011-2022 走看看