zull异常处理

zull异常处理
一、网关内部逻辑异常自定义
通常zull走默认异常zullException,过滤器内部异常通过改变requst中异常信息属性如下代码:

private void exceptionOperation(HttpServletRequest request ,ResultCode resultCode){

    request.setAttribute("javax.servlet.error.code", resultCode.code());
    request.setAttribute("javax.servlet.error.msg", resultCode.message());
    ExceptionCast.cast(CommonCode.USERINFO_ERROR);

}
public class ExceptionCast {

public static void cast(ResultCode resultCode){
    throw new CustomException(resultCode);
}

}
自定义异常CustomException继承RuntimeException,如下:
public class CustomException extends RuntimeException {

//错误代码
ResultCode resultCode;
public CustomException(ResultCode resultCode){
    this.resultCode = resultCode;
}
public ResultCode getResultCode(){
    return resultCode;
}

}
二、网关转发至微服务时,未找到服务异常处理。
如下“XbzkServiceList.XB_SERVICE_SYSTEM”为具体微服务id

@Component
public class SystemFallbackProvider implements FallbackProvider {

@Override
public String getRoute() {
    return XbzkServiceList.XB_SERVICE_SYSTEM;
}

@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    return new ClientHttpResponse() {
        @Override
        public HttpStatus getStatusCode() throws IOException {
            return HttpStatus.OK;
        }

        @Override
        public int getRawStatusCode() throws IOException {
            return HttpStatus.OK.value();
        }
        @Override
        public String getStatusText() throws IOException {
            return HttpStatus.OK.getReasonPhrase();
        }
        @Override
        public void close() {
        }
        @Override
        public InputStream getBody() throws IOException {
            String response = "{\n" +
                    "    \"msg\": \"system服务请求失败,请联系管理员\",\n" +
                    "    \"code\": 500,\n" +
                    "    \"data\": null\n" +
                    "}";
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBytes());
            return byteArrayInputStream;
        }
        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            return httpHeaders;
        }
    };
}

}
其次需要过滤/error,
@RestController
public class ErrorHandlerController implements ErrorController {

/**
 * 出异常后进入该方法,交由下面的方法处理
 */
@Override
public String getErrorPath() {
    return "/error";
}
@RequestMapping("/error")
public ResponseResult error(HttpServletRequest request) {
    String message = request.getAttribute("javax.servlet.error.msg").toString();
    int code = Integer.valueOf(request.getAttribute("javax.servlet.error.code").toString());
    return new ResponseResult(false,code,message);
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值