SpringBoot

springboot 统一错误处理

2018-08-23  本文已影响35人  东方舵手

方式一 : 继承BasicErrorController

1. 创建配置类

import com.imooc.manager.error.MyErrorController;
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;

import java.util.List;

/**
 * 错误处理相关配置
 */
@Configuration
public class ErrorConfiguration {

    @Bean
    public MyErrorController basicErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties,
                                                  ObjectProvider<List<ErrorViewResolver>> errorViewResolversProvider) {
        return new MyErrorController(errorAttributes, serverProperties.getError(),errorViewResolversProvider.getIfAvailable());
    }

}

2. 创建自定义处理类 并继承BasicErrorController

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;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 自定义错误处理controller
 */
public class MyErrorController extends BasicErrorController {

    public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
        super(errorAttributes, errorProperties, errorViewResolvers);
    }

    /*
    springboot默认返回的错处信息
    {
    "timestamp": "2018-08-23 20:33:12",
    "status": 500,
    "error": "Internal Server Error",
    "message": "编号不可为空",
    "path": "/manager/products",
    }
    */

    //getErrorAttributes 方法可以获取,或者设置这些内容
    @Override
    protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
        Map<String, Object> attrMap = super.getErrorAttributes(request, includeStackTrace);
        //去除掉默认返回的错误信息
        attrMap.remove("timestamp");
        attrMap.remove("status");
        attrMap.remove("error");
        attrMap.remove("exception");
        attrMap.remove("path");
        //返回自定义的错误信息
        String errorCode = (String) attrMap.get("message");
        ErrorEnum errorEnum = ErrorEnum.getByCode(errorCode);
        attrMap.put("message",errorEnum.getMessage());
        attrMap.put("code",errorEnum.getCode());
        attrMap.put("canRetry",errorEnum.isCanRetry());
        return attrMap;
    }
}

方式二:使用注解@ControllerAdvice

1. 创建自定类 添加注解

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

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

/**
 * 统一错误处理
 */
@RestControllerAdvice(basePackages = {"com.acc.manager.controller"})
public class ErrorControllerAdvice {

    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e){
        Map<String, Object> attrMap = new HashMap<>();
        String errorCode = e.getMessage();
        ErrorEnum errorEnum = ErrorEnum.getByCode(errorCode);
        attrMap.put("message",errorEnum.getMessage());
        attrMap.put("code",errorEnum.getCode());
        attrMap.put("canRetry",errorEnum.isCanRetry());
        attrMap.put("type","advice");
        return new ResponseEntity(attrMap, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
上一篇 下一篇

猜你喜欢

热点阅读