Jetty 12: forward to error page on invalid URLs - Stack Overflow

admin2025-04-29  3

I recently migrated my application from Jetty 11 to Jetty 12 and noticed a change in behavior when handling invalid URLs.

Details: My application uses Spring, which utilizes JettyEmbeddedErrorHandler (an extension of ErrorPageErrorHandler) as the ErrorHandler.

In Jetty 11, when I attempted to access an invalid URL, JettyEmbeddedErrorHandler would forward the request to an error page.

However, in Jetty 12, accessing an invalid URL no longer results in forwarding to the error page. Upon investigation, I found that the bad URL exception is handled by a different mechanism—it is processed by the "server’s" error handler instead of the "servlet’s" error handler. This server-level handling means it cannot interact with the servlet request, servlet dispatcher, etc.

I need to replicate the Jetty 11 behavior in Jetty 12: forwarding requests to the error page for invalid URLs.

I've done some research but haven't found a solution. I came across ReHandlingErrorHandler, but I'm unsure if it suits my needs and couldn't find any examples of its usage.

Could someone advise whether it’s possible to achieve this? If so, could you provide examples or guidance?

---- UPDATE ----

Look likes I've configured ErrorHandler, but I'm unsure that it is correct way to use it. I have the following configuration

    private static class SilentErrorHandler extends org.eclipse.jetty.server.handler.ReHandlingErrorHandler {
        
        @Override
        protected String getReHandlePathInContext(
            Request request,
            int code,
            Throwable cause
        ) {
            return "/error";
        }

        SilentErrorHandler(Handler handler) {
            super(handler);
            setShowStacks(false);
            setShowMessageInTitle(false);
        }
    }
    
    @Bean
    public JettyServerCustomizer jettyServerCustomizer(
        RequestLog requestLog
    ) {
        return server -> {
            server.setErrorHandler(new SilentErrorHandler(server.getHandler()));
            server.setRequestLog(requestLog);
    ....
    }

Look like it redirects to error page now, but I need to check that everything is correct.

---- UPDATE 2 ----

I reviewed the solution mentioned earlier and found an issue: it does not forward the original URI and headers to the handler, and consequently, to my error page. This is problematic because the response on the error page depends on the Accept header.

How can I ensure that the same URI and headers are passed to the error handler so that my error page functions correctly?

转载请注明原文地址:http://anycun.com/QandA/1745942096a91435.html