1. 程式人生 > >SpringBoot-異常處理的另一種方式

SpringBoot-異常處理的另一種方式

1、實現BasicErrorController類

package com.imooc.error;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;

/**
 * 自定義錯誤處理controller
 * @author 
 *
 */
public class MyErrorController extends BasicErrorController{

	public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
		super(errorAttributes, errorProperties, errorViewResolvers);
	}
	
	
	@Override
	protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
		// TODO Auto-generated method stub
		Map<String, Object>  attrs = super.getErrorAttributes(request, includeStackTrace);
		attrs.remove("timestamp");
		attrs.remove("status");
		attrs.remove("error");
		attrs.remove("exception");
		attrs.remove("path");
		String errorCode = (String)attrs.get("message");
		ErrorEnum errorEnum = ErrorEnum.getByCode(errorCode);
		attrs.put("message", errorEnum.getMessage());
		attrs.put("code", errorEnum.getCode());
		attrs.put("canRetry", errorEnum.isCanRetry());
		return attrs;
	}
}

2、異常配之類

package com.imooc.error;

import java.util.List;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 錯誤處理相關配置
 * @author 
 *
 */
@Configuration
public class ErrorConfig {

	@Bean
	public MyErrorController basicErrorController(ErrorAttributes errorAttributes
			, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers ){
		
		return new MyErrorController(errorAttributes, errorProperties, errorViewResolvers);
	}
}

3、異常列舉

package com.imooc.error;

/**
 * 錯誤種類
 * @author zemel	
 *
 */
public enum ErrorEnum {
	
	ID_NOT_NULL("F001", "編號不能為空", false),
	UNKONW("9999", "未知異常", false);
	
	private String code;
	private String message;
	private boolean canRetry;
	
	ErrorEnum(String code, String message, boolean canRetry) {
		this.code = code;
		this.message = message;
		this.canRetry = canRetry;
	}
	
	public static ErrorEnum getByCode(String code){
		for(ErrorEnum errorEnum : ErrorEnum.values()){
			if(errorEnum.code.equals(code)){
				return errorEnum;
			}
		}
		return UNKONW;
	}

	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;
	}

	public boolean isCanRetry() {
		return canRetry;
	}

	public void setCanRetry(boolean canRetry) {
		this.canRetry = canRetry;
	}
}

4、邏輯流程

SpringBoot預設處理異常的類是BasicErrorController,

當程式發生異常時,會屌用此介面getErrorAttributes,

然後MyErrorController重寫該方法,根據Java中的多型原理,

主程將屌用MyErrorController的getErrorAttributes,

其中的業務邏輯就是主要的實現邏輯。