zuulfilter添加例外,自定义Zuul异常

博客围绕Zuul过滤器展开,当URL路由的服务可能宕机时,会返回500 HTTP状态和ZuulException的JSON响应。作者尝试自定义或移除JSON响应、更改HTTP状态码,通过扩展Zuul Filter实现,最终给出了CustomErrorFilter类的解决方案。

I have a scenario in Zuul where the service that the URL is routed too might be down . So the reponse body gets thrown with 500 HTTP Status and ZuulException in the JSON body response.

{

"timestamp": 1459973637928,

"status": 500,

"error": "Internal Server Error",

"exception": "com.netflix.zuul.exception.ZuulException",

"message": "Forwarding error"

}

All I want to do is to customise or remove the JSON response and maybe change the HTTP status Code.

I tried to create a exception Handler with @ControllerAdvice but the exception is not grabbed by the handler.

UPDATES:

So I extended the Zuul Filter I can see it getting into the run method after the error has been executed how do i change the response then. Below is what i got so far. I read somewhere about SendErrorFilter but how do i implement that and what does it do?

public class CustomFilter extends ZuulFilter {

@Override

public String filterType() {

return "post";

}

@Override

public int filterOrder() {

return 1;

}

@Override

public boolean shouldFilter() {

return true;

}

@Override

public Object run() {

final RequestContext ctx = RequestContext.getCurrentContext();

final HttpServletResponse response = ctx.getResponse();

if (HttpStatus.INTERNAL_SERVER_ERROR.value() == ctx.getResponse().getStatus()) {

try {

response.sendError(404, "Error Error"); //trying to change the response will need to throw a JSON body.

} catch (final IOException e) {

e.printStackTrace();

} ;

}

return null;

}

Added this to the class that has @EnableZuulProxy

@Bean

public CustomFilter customFilter() {

return new CustomFilter();

}

解决方案

We finally got this working [Coded by one of my colleague]:-

public class CustomErrorFilter extends ZuulFilter {

private static final Logger LOG = LoggerFactory.getLogger(CustomErrorFilter.class);

@Override

public String filterType() {

return "post";

}

@Override

public int filterOrder() {

return -1; // Needs to run before SendErrorFilter which has filterOrder == 0

}

@Override

public boolean shouldFilter() {

// only forward to errorPath if it hasn't been forwarded to already

return RequestContext.getCurrentContext().containsKey("error.status_code");

}

@Override

public Object run() {

try {

RequestContext ctx = RequestContext.getCurrentContext();

Object e = ctx.get("error.exception");

if (e != null && e instanceof ZuulException) {

ZuulException zuulException = (ZuulException)e;

LOG.error("Zuul failure detected: " + zuulException.getMessage(), zuulException);

// Remove error code to prevent further error handling in follow up filters

ctx.remove("error.status_code");

// Populate context with new response values

ctx.setResponseBody(“Overriding Zuul Exception Body”);

ctx.getResponse().setContentType("application/json");

ctx.setResponseStatusCode(500); //Can set any error code as excepted

}

}

catch (Exception ex) {

LOG.error("Exception filtering in custom error filter", ex);

ReflectionUtils.rethrowRuntimeException(ex);

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值