【问题标题】:create stand error page for zuul [version 1.1.2]为 zuul [版本 1.1.2] 创建支架错误页面
【发布时间】: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


【解决方案1】:

我将不得不花一些时间来看看您如何从我正在旅行的错误页面中阅读。但是通过阅读内容来设置响应体可能不是一个坏选择,但绝对可能并不完美。 如果有帮助,请查看以下代码。

还可以添加一些您可能正在尝试但无法以更轻松的方式回答和帮助的代码。 您需要确保过滤器在预装饰过滤器之后运行。

  @Override
        public Object run() {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();

        String url = UriComponentsBuilder.fromHttpUrl("http://localhost:8082").path("/outage").build()
                                     .toUriString();
        ctx.set("requestURI", url);
        return null;
    }

查看此以获取更多信息:- https://github.com/spring-cloud/spring-cloud-netflix/issues/1754

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多