问题描述
在common模块增加统一异常处理代码,如下。在service业务处理类中抛出异常,
 但是接口返回的为spring统一的500错误。
package com.tea.common.exception;
import com.tea.common.entity.ResponseResult;
 import com.tea.common.entity.StatusCode;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: BaseExceptionHandler
* @author: junfeng
* @date: 2022/11/25
**/
@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseResult<Object> error(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseResult.fail(StatusCode.ERROR);
    }
    @ExceptionHandler(value = NormalException.class)
    @ResponseBody
    public ResponseResult<Object> error(NormalException e) {
        log.error(e.getMessage(), e);
        return ResponseResult.fail(StatusCode.ERROR, e.getMessage());
    }
}
 
分析
这是因为这个类没有交给IOC容器管理!---- 根本就没有扫描到这个包
 testException这个类是全局异常处理类,需要需要交给ioc容器管理,故需要确保能扫描到这个类!且观察类上午spring注解标识。
解决办法
将此统一异常类注入到spring.factories即可
 



















