SpringBoot-异常处理的另一种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangminemail/article/details/83187744

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.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ServerProperties;
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, ServerProperties serverProperties,
            ObjectProvider<List<ErrorViewResolver>> errorViewResolversProvider){
		
		return new MyErrorController(errorAttributes, serverProperties.getError(), errorViewResolversProvider.getIfAvailable());
	}
}

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,

其中的业务逻辑就是主要的实现逻辑。

猜你喜欢

转载自blog.csdn.net/zhangminemail/article/details/83187744