dubbo异常捕捉,利用spring全局捕捉并返回调用处

在业务处理逻辑中抛出自定义异常

public Object MethodSelf{ 
    ......
    throw new SelfException(JSONObject.fromObject("{"key1":"value1"}", StringUtil.jsonConfig).toString());
}

public class SelfException extends RuntimeException {
    /** serialVersionUID :  */
    private static final long serialVersionUID = -4172267101901373754L;
    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public SelfException(String message) {
        super();
        this.message = message;
    }
}

spring.xml

<bean id="exceptionResolver" class="com.*.resolver.CustomExceptionResolver"></bean>
public class CustomExceptionResolver implements HandlerExceptionResolver {
    private static Logger logger = LoggerFactory.getLogger(CustomExceptionResolver.class);
    //wise自定义错误 throw 后需要截取exception.message中有效的json信息
    private static final int SUBSTRLENG="com.*.SelfException : ".length();
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
        ModelAndView modelAndView = new ModelAndView();
        response.setStatus(HttpStatus.OK.value()); //设置状态码
        response.setContentType(MediaType.APPLICATION_JSON_VALUE); //设置ContentType
        response.setCharacterEncoding("UTF-8"); //避免乱码
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        try {
            if (exception instanceof SelfException) {
                SelfException selfException= (SelfException) exception;
                response.getWriter().write(selfException.getMessage());
                logger.error("exceptions:" ,exception);
            } else if (exception.getMessage().contains("SelfException:")) { //中台自定义错误异常
                String exceptionMessages[] = exception.getMessage().split("\n");
                if (exceptionMessages.length >= 2) {
                    if (exceptionMessages[0].length() >= SUBSTRLENG) {
                        String wiseExceptionMes = exceptionMessages[0].substring(SUBSTRLENG);
                        logger.info("WiseException={}", StringUtil.formatJson(JSONObject.fromObject(wiseExceptionMes).toString()));
                        response.getWriter().write(wiseExceptionMes);
                    }
                }
                logger.error("exceptions:", exception);
            } else {
               
                }
                response.getWriter().write(JSONObject.fromObject("{"key1":"value1"}").toString());
               
            }

        } catch (Exception e) {
            logger.error("全局异常处理失败", e);
        }
        return modelAndView;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值