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};其它示例也都是預想中結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM