SpringBoot-2.X 學習筆記01 全局/自定義異常處理

SpringBoot-2.* 學習筆記01 全局/自定義異常處理


1 全局異常處理 返回 Json 數據,如果要返回指定錯誤頁面,參考:2 自定義異常處理

package com.xu.springboot.utils;

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

import javax.servlet.http.HttpServletRequest;

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.servlet.ModelAndView;

/**
 * SprinBoot-2.* 異常處理
 * @ClassName: ExceptionHander.java  
 * @Description: SprinBoot-2.* 全局/自定義異常處理  
 * @author: hyacinth
 * @date: 2019年7月29日 下午9:38:46     
 * @Copyright: hyacinth
 */
@ControllerAdvice
public class ExceptionHander {
	
	/**
	 * SprinBoot-2.* 全局異常處理  
	 * @Title: handerException   
	 * @Description: 全局異常處理
	 * @param: exception 異常
	 * @param: request 請求   
	 * @return: Object 返回Json
	 * @date: 2019年7月31日 下午10:15:11    
	 * @throws
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody// 全局異常處理---返回Json
	public Object handerException(Exception exception,HttpServletRequest request) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("code", 200);
		map.put("message", exception.getMessage());
		map.put("url", request.getRequestURI());
		return map;
	}

}

2 自定義異常處理
2.1 新建一個自定義異常類繼承RuntimeException

package com.xu.springboot.utils;

/**
 * SprinBoot-2.* 自定義異常處理
 * @ClassName: ExceptionEntity   
 * @Description: TODO   
 * @author: hyacinth
 * @date: 2019年7月29日 下午9:37:39     
 * @Copyright: hyacinth
 */
public class MyException extends RuntimeException{

	/**   
	 * @Fields serialVersionUID:TODO
	 * @Date 2019年7月29日 下午9:35:19
	 */ 
	private static final long serialVersionUID = -8810233016579430240L;

	private String code;
	
	private String message;

	public MyException(String code,String message) {
		this.code = code;
		this.message = message;
	}
	
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	@Override
	public String toString() {
		return "MyException [code=" + code + ", message=" + message + "]";
	}
	
}

2.2 異常處理controller

package com.xu.springboot.utils;

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

import javax.servlet.http.HttpServletRequest;

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.servlet.ModelAndView;

/**
 * SprinBoot-2.* 異常處理
 * @ClassName: ExceptionHander.java  
 * @Description: SprinBoot-2.* 全局/自定義異常處理  
 * @author: hyacinth
 * @date: 2019年7月29日 下午9:38:46     
 * @Copyright: hyacinth
 */
@ControllerAdvice
public class ExceptionHander {
	
	/**
	 * SprinBoot-2.* 自定義異常處理  
	 * @Title: handerMyException   
	 * @Description: 自定義異常處理
	 * @param: exception 自定義異常
	 * @return: Object 返回頁面
	 * @date: 2019年7月31日 下午10:15:11    
	 * @throws
	 */
	@ExceptionHandler(value = MyException.class)// 自定義異常處理---返回頁面
	public Object handerMyException(MyException exception) {
		ModelAndView view=new ModelAndView();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("code", exception.getCode());
		map.put("message", exception.getMessage());
		view.addObject("msg", map);
		view.setViewName("/login");//自定義錯誤頁面
		return view;
	}
	
}

3 異常處理測試類

package com.xu.springboot.controler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xu.springboot.utils.MyException;

@Controller
@RequestMapping("/exception")
public class ExceptionController {
	
	@RequestMapping("/test01")
	public void test1() {//1 全局異常
		int a=1/0;
	}
	
	@RequestMapping("/test02")
	public void test2() {//2 自定義異常
		try {
			int a=1/0;
		} catch (Exception e) {
			throw new MyException("300", e.getMessage());//拋出自定義異常類
		}
	}
	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章