如何在不为每个资源定义默认处理程序的情况下覆盖所有404页? [英] How do I override all 404 pages without defining default handlers for every resource?

查看:65
本文介绍了如何在不为每个资源定义默认处理程序的情况下覆盖所有404页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Dropwizard项目中,我定义了一个通用ExceptionMapper<WebApplicationException>:

In my Dropwizard project, I'm defining a generic ExceptionMapper<WebApplicationException>:

environment.jersey().register(new WebApplicationExceptionMapper());

...但是,对于不匹配的路径,这似乎并没有捕获任何404错误.

...but this doesn't seem to catch any of the 404 errors for unmatched paths.

在没有为每个资源定义五个defaultHandler()方法的情况下,如何捕获所有404,以便可以返回自己的错误页面或某些JSON?

Without defining five defaultHandler() methods for every single resource, how do I catch all 404s so I can return my own error page or some JSON?

因此,如果我提供的一项服务只有一个资源,例如/docs,这就是情况:

So, if I had a service with one resrouce, say, /docs, this is the situtation:

  • /myservice/docs/helloworld与我的DocsResource中定义的任何@Path不匹配.它返回一个通用的Jetty 404页面(不是我想要的)

  • /myservice/docs/helloworld doesn't match any @Path defined in my DocsResource. It returns a generic Jetty 404 page (not what I want)

/myservice/doesntexist使用异常映射器返回我自己的错误资源(这是我到处都想要的)

/myservice/doesntexist returns my own error resource with the exception mapper (this is what I want everywhere)

推荐答案

您需要做的是设置一个不同的Error处理程序.碰到不存在的路径时看到的404不是由球衣处理的.泽西岛为资源映射异常,但是实际上您从来没有打过资源.这是您需要定义错误处理程序的地方:

what you need to do is to set a different Error handler. The 404's you are seeing when hitting a non-existing path, are not handled by jersey. Jersey maps exceptions for resources, but you in fact never hit the resource in the first place. This is where you will need to define an error handler:

在DefaultServerFactory中,您需要覆盖:

In DefaultServerFactory, you need to overwrite:

protected Server buildServer(LifecycleEnvironment lifecycle,
                                 ThreadPool threadPool) {
        final Server server = new Server(threadPool);
        server.addLifeCycleListener(buildSetUIDListener());
        lifecycle.attach(server);
        final ErrorHandler errorHandler = new ErrorHandler();
        errorHandler.setServer(server);
        errorHandler.setShowStacks(false);
        server.addBean(errorHandler);
        server.setStopAtShutdown(true);
        server.setStopTimeout(shutdownGracePeriod.toMilliseconds());
        return server;
    }

(此类在AbstractServerFactory中).

(This class is in AbstractServerFactory).

然后,您可以实现自己的ErrorHandler并使它执行您想要执行的任何操作.

You then can implement your own ErrorHandler and make it do whatever it is you want it to do.

要进行测试,请打开org.eclipse.jetty.server.handler.ErrorHandler并设置一个断点,然后尝试输入不存在的URL.它会停在那里,您可以看到码头如何处理这种事情.

For testing, open org.eclipse.jetty.server.handler.ErrorHandler and set a breakpoint, then try and hit a non-existing URL. It will stop there and you can see how jetty handles this sort of thing.

我希望能帮上忙.

如果您需要覆盖默认DW功能的帮助,可以查看类似的帖子,其中我描述了如何覆盖记录器:

If you need help overwriting default DW functionality, you can look at a similar post where I described how to overwrite loggers:

Dropwizard不会将自定义记录器记录到文件中

干杯, 阿图尔(Artur)

Cheers, Artur

这篇关于如何在不为每个资源定义默认处理程序的情况下覆盖所有404页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆