【发布时间】:2017-06-15 08:05:50
【问题描述】:
我正在尝试为 zuul 服务器创建标准错误页面,以便我可以将异常重定向到此页面?
目前,我已经创建了一个zuul过滤器来捕获zuul异常,如下所示:
代码 sn-ps
@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("Internal Server Error : Please contact Phoenix Admin");
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;
}
感谢任何建议?
【问题讨论】:
-
将内容类型更改为 text/html 应该会抛出一个页面而不是 json。那是你要找的吗?或者你想抛出一个实际的页面。
-
我希望内容应该从错误页面中读取,而不是直接硬编码到响应正文中,我正在考虑的一个选项是编写一段代码来从文件中读取内容并填充到 responseBody , 但我相信会有更好的方法来点缀它@GrinishNepal
标签: netflix-zuul