当前位置:   article > 正文

Spring Boot FeignClient 捕获业务异常信息_feign调用接口捕获异常

feign调用接口捕获异常
  • 因项目重构采用spring cloud,feign不可避免。目前spring
    cloud在国内还不是很成熟,所以踩坑是免不了的。最近处理全局异常的问题,搜了个遍也没找到合适的解决方案

1.全局异常处理


import com.bossien.common.comm.entity.ResponseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * @Author: lixg
     * @Description: 系统异常捕获处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseDto errorExceptionHandler(Exception ex) {//APIResponse是项目中对外统一的出口封装,可以根据自身项目的需求做相应更改
        logger.error("捕获到 Exception 异常", ex);
        //异常日志入库
        return new ResponseDto(ResponseDto.RESPONSE_FAIL, "系统繁忙,请稍后再试");
    }

    /**
     * @Author: lixg
     * @Description: 自定义异常捕获处理
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)//BusinessException是自定义的一个异常
    public ResponseDto businessExceptionHandler(BusinessException ex) {
        logger.error("捕获到 BusinessException 异常: code=" + ex.getCode() + " , errorMessage=" + ex.getErrorMessage());
        return new ResponseDto(ex.getCode(), ex.getErrorMessage());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

2.请求参数解析handler

import com.alibaba.fastjson.JSONObject;
import com.ocean.common.comm.entity.ResponseDto;
import com.ocean.common.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * @author lixg
 *
 * feign请求响应对象处理
 */
public class ResponseHandler {
    private final static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);

    /**
     * 解析请求响应对象
     * @param responseDto
     * @param clazz
     * @return
     * @throws BusinessException
     */
    public static Object getResponseData(ResponseDto responseDto, Class clazz) throws BusinessException {
        if(EmptyUtil.isEmpty(responseDto)){
            throw new BusinessException(BusinessException.OBJECT_IS_NULL,"请求响应为空!");
        }
        if(ResponseDto.RESPONSE_SUCCESS.equals(responseDto.getCode())){
            try {
                String json = JSONObject.toJSONString(responseDto.getData());
                return JSONObject.parseObject(json, clazz);
            }catch (Exception e){
                logger.error("响应对象转换异常:"+clazz.getName(),e);
                throw new BusinessException(BusinessException.OBJECT_IS_NULL,"响应对象转换失败!");
            }
        }else{
            throw new BusinessException(responseDto.getCode(),responseDto.getMessage());
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

3.业务feign接口

package com.bossien.usercenter.user.feign;

import com.bossien.common.comm.entity.ResponseDto;
import com.bossien.common.comm.util.PageModel;
import com.bossien.common.comm.constant.SearchEntity;
import com.bossien.common.core.exception.BusinessException;
import com.bossien.usercenter.user.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

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

@FeignClient(value="bossien-usercenter-service",path = "/userFeign")
@Repository
public interface UserFeign {

    @RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
    User getUserInfo(@RequestParam("userId") Long userId);

    @RequestMapping(value = "getUserInfoByTicket",method = RequestMethod.GET)
    ResponseDto getUserInfoByTicket(@RequestParam("ticket") String ticket) throws BusinessException;
 
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

总结:
@controllerAdvice或者HandlerExceptionResolver是不能直接捕获到FeignException,所以需要在Feign层面拿到具体异常重新封装。最后总算把cloud service内部的异常安全(一样的错误码、一样的错误信息)送给了client!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/84609
推荐阅读
相关标签
  

闽ICP备14008679号