Spring Boot 统一异常处理+对返回客户端之前进行统一处理

有时候我们经常遇到逻辑正常执行时返回客户端指定格式的数据,比如json,但是遇NullPointerException空指针异常,NoSuchMethodException调用的方法不存在异常,返回给客户端的是服务端异常堆栈信息,导致客户端不能正常解析数据;这明显不是我们想要的;或者我们已经用@RestControllerAdvice+ @ExceptionHandler进行统一异常处理,但是每次返回的时候都要写上return new RestRest().success(data)这里需要用到ResponseBodyAdvice在返回客户端之前进行统一处理。


  • 先记录一下统一异常处理:
    1. 统一异常处理类
@Slf4j
@RestControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {ParameterException.class})
    public ErrorInfo parameterHandler(HttpServletRequest request, Exception exception) {

        log.info("参数检测异常");
        ErrorInfo errorInfo = new ErrorInfo<>();
        errorInfo.setCode(ErrorCodeInfoEnum.PARAM_ERROR.getCode());
        errorInfo.setMsg(ErrorCodeInfoEnum.PARAM_ERROR.getMsg());
        errorInfo.setUrl(request.getRequestURI());
        errorInfo.setData(exception);
        log.info("异常信息:{}", errorInfo.toString());
        return errorInfo;
    }

    @ExceptionHandler(value = {Exception.class})
    public ErrorInfo defaultExceptionHandler(HttpServletRequest request, Exception exception) {

        log.info("默认异常处理");
        ErrorInfo errorInfo = new ErrorInfo<>();
        errorInfo.setMsg(exception.getMessage());
        errorInfo.setUrl(request.getRequestURI());
        errorInfo.setData(exception);
        log.info("默认异常处理信息:{}", errorInfo.toString());
        return errorInfo;
    }

    @ExceptionHandler(value = NullPointerException.class)
    private ErrorInfo nullPointerExceptionHandler(HttpServletRequest request, Exception exception) {
        log.info("null异常处理");
        ErrorInfo errorInfo = new ErrorInfo<>();
        errorInfo.setMessage("空指针异常");
        errorInfo.setStatus(2);
        errorInfo.setData(exception);
        errorInfo.setUrl(request.getRequestURI());
        log.info("空指针异常", exception);
        return errorInfo;
    }

    @ExceptionHandler(value = NoPermissionException.class)
    private ErrorInfo noPermissionExceptionHandler(HttpServletRequest request, NoPermissionException exception) {
        ErrorInfo errorInfo = new ErrorInfo<>();
        errorInfo.setCode(ErrorCodeInfoEnum.NO_PERMISSION.getCode());
        errorInfo.setMsg(ErrorCodeInfoEnum.NO_PERMISSION.getMsg());
        errorInfo.setUrl(request.getRequestURI());
        errorInfo.setData(exception);
        log.info("没有权限异常", exception);
        return errorInfo;
    }
}
 
 
  1. 自定义异常类
@Getter
public class NoPermissionException extends RuntimeException {
    public NoPermissionException() {
    }

    public NoPermissionException(String message) {
        super(message);
    }

    public NoPermissionException(String message, Throwable cause) {
        super(message, cause);
    }

    public NoPermissionException(Throwable cause) {
        super(cause);
    }

    public NoPermissionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

  1. 用于对返回结果进行统一封装
@Data
@NoArgsConstructor
public class RestResult {

    /**
     * 请求是否成功
     */
    private boolean success;

    /**
     * 请求状态吗
     */
    private Integer code;

    /**
     * 请求返回信息描述
     */
    private String msg;

    /**
     * 响应数据
     */
    private Object data;

    public RestResult(boolean success, String msg, Object data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }
}
  1. 错误信息枚举
@Getter
public enum ErrorCodeInfoEnum {

    PARAM_ERROR(1, "参数不正确"),

    NO_PERMISSION(0,"没有权限")

    ;
    
    private Integer code;
    private String msg;

    ErrorCodeInfoEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}
  1. 业务层具体使用时抛出异常,等待统一处理
 @Override
 public LostVo queryLostVo(String lostId) {

        log.info("lostId:{}", lostId);

        if (!StringUtils.isNotEmpty(lostId)) {

            throw new ParameterException("请检查参数是否正确");
        } else {

            Lost lost = lostMapper.selectByPrimaryKey(lostId);

            User user = userMapper.selectByOpenId(lost.getLostOpenid());

            LostVo lostVo = new LostVo();
            …………
        }
//

::: warning ResponseBodyAdvice
下面记录一下对返回结果的统一处理
:::


public interface ResponseBodyAdvice {
 
boolean supports(MethodParameter returnType, Class> converterType);
 
T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,
        Class> selectedConverterType,
        ServerHttpRequest request, ServerHttpResponse response);
}

@Slf4j
@RestControllerAdvice(basePackages = {"com.gyx.lostandfound.controller"})
public class MyResponseBodyAdvice implements ResponseBodyAdvice {

    /**
     * 判断支持的类型
     * @param returnType
     * @param converterType
     * @return
     */
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
    }

    /**
     * 对结果进行封装,或者进行加密处理
     * @param body
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body == null) {
            return body;
        } else if (body instanceof ErrorInfo) {
            RestResult restResult = new RestResult();
            restResult.setSuccess(false);
            restResult.setCode(-1);
            restResult.setMsg("出现异常");
            restResult.setData(body);
            return body;
        } else if(body instanceof  String){
            return body;
        } else {
            RestResult restResult = new RestResult();
            restResult.setSuccess(true);
            restResult.setCode(200);
            restResult.setData(body);
            body = restResult;
            return body;
        }
    }
}

controller层代码:

    @ApiOperation(value = "用于查询自己所发布的物品信息")
    @GetMapping(value = "query")
    public Page selectRelease(@ApiParam(value = "用户openId") @RequestParam String openId,
                                      @ApiParam(value = "状态") @RequestParam Integer status,
                                      @ApiParam(value = "第几页") @RequestParam int pageIndex,
                                      @ApiParam(value = "每页有多少个") @RequestParam int pageSize){


        return userService.selectRelease(openId,status,pageIndex,pageSize);
 
 

利用postman测试一下:
输入url:localhost:8988/user/query?openId=111&status=1&pageIndex=1&pageSize=5
返回结果:

{
    "success": true,
    "code": 200,
    "msg": null,
    "data": {
        "pageNum": 1,
        "pageSize": 5,
        "total": 0,
        "pages": 1,
        "list": []
    }
}

最后补充一下目录结构:

avatar

你可能感兴趣的:(Spring Boot 统一异常处理+对返回客户端之前进行统一处理)