springjerseyjetty

Spring6, Jersey3, Jetty12, SEVERE: Spring context lookup failed, skipping spring component provider initialization


I have a java 21 spring6 framework (not boot) application. The app is a simple embedded web server using jetty 12 as the server, and jersey 3 as the resource handler. It works. I've written integration tests that start up the server and hit a few endpoints with rest-assured client calls. They work perfectly.

I cannot for the life of me figure out how to get rid of this error:

Nov 09, 2024 9:05:22 PM org.glassfish.jersey.server.spring.SpringComponentProvider initialize
SEVERE: Spring context lookup failed, skipping spring component provider initialization.

I've tried every combination of registration and property settings I can think of. How can I tell jersey "Here's your spring context; stop complaining."

Here's my code:

    public int startServer(int port) throws Exception {
        this.context = new AnnotationConfigApplicationContext(AppConfig.class);

        ResourceConfig rc = new ResourceConfig()
                .packages("com.example.resources")
                .register(JerseyLoggingFilter.class)
                .property(ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);

        InetSocketAddress bindAddr = new InetSocketAddress(BIND_ADDR, port);
        this.server = new Server(bindAddr);
        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        contextHandler.setContextPath("/");
        ServletHolder servletHolder = new ServletHolder(new ServletContainer(rc));
        contextHandler.addServlet(servletHolder, "/*");
        this.server.setHandler(contextHandler);
        this.server.start();
        port = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
        log.info("server listening on port " + port);
        return port;
    }

Solution

  • Thanks to @M.Deinum for the comment - it guided me to a solution:

        public int startServer(int port) throws Exception {
            this.context = new AnnotationConfigWebApplicationContext();
            this.context.register(AppConfig.class);
    
            ResourceConfig rc = new ResourceConfig()
                    .packages("com.primarydata.proxy")
                    .register(JerseyLoggingFilter.class)
                    .property(ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);
    
            InetSocketAddress bindAddr = new InetSocketAddress(BIND_ADDR, port);
            this.server = new Server(bindAddr);
            ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
            contextHandler.addEventListener(new ContextLoaderListener(context));
            contextHandler.setContextPath("/");
            ServletHolder servletHolder = new ServletHolder(new ServletContainer(rc));
            contextHandler.addServlet(servletHolder, "/*");
            this.server.setHandler(contextHandler);
            this.server.start();
    
            int actualPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
            return actualPort;
        }
    

    It should be noted that once the web app context has been registered with the Jetty server, it's no longer necessary to call stop on it. Calling server.stop() is sufficient.