自定义 Zuul 异常 [英] Customizing Zuul Exception
问题描述
我在 Zuul 中有一个场景,其中 URL 路由的服务也可能关闭.因此,响应正文在 JSON 正文响应中被抛出 500 HTTP 状态和 ZuulException.
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"
}
我想要做的就是自定义或删除 JSON 响应,并可能更改 HTTP 状态代码.
All I want to do is to customise or remove the JSON response and maybe change the HTTP status Code.
我尝试使用@ControllerAdvice 创建异常处理程序,但处理程序未捕获异常.
I tried to create a exception Handler with @ControllerAdvice but the exception is not grabbed by the handler.
更新:
所以我扩展了 Zuul 过滤器,我可以看到它在执行错误后进入 run 方法,然后我该如何更改响应.以下是我到目前为止所得到的.我在某处阅读了有关 SendErrorFilter 的内容,但我如何实现它以及它的作用是什么?
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;
}
将此添加到具有@EnableZuulProxy 的类
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;
}
}
这篇关于自定义 Zuul 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!