Migrating Jetty to SpringBoot Jetty

Viewed 685

I have an app that uses spring 4.x, and decided to migrate it to spring 5.x with spring boot 2.3.0.

The app is running on top of the jetty server that has some special configuration and quite a few connectors. I managed to get the hold of the embedded jetty server from spring and add the connectors, but I am struggling with setting some of the specific values.

I also moved part of the xml configuration to annotated classes - so let me explain the problem.

If i understand correctly, my configuration class should implement these interfaces:

@Configuration
public class Server implements WebServerFactoryCustomizer<JettyServletWebServerFactory>,
                               ServletContextInitializer

I implement the void customize(JettyServletWebServerFactory factory) method from WebServerFactoryCustomizer where I add connectors and set some values.

That looks like this:

void customize(JettyServletWebServerFactory factory) {
   factory.addServerCustomizers(jettyServer -> {
       jettyServer.addConnector(new ServerConnector(server, sslContextFactory));
       ...

       // ---the part of the old code---
                final ServletHolder servletHolder = new ServletHolder(new CXFServlet());
        final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

        context .setContextPath("/");
        context .addServlet(servletHolder, "/*");
        context .addFilter(MyCustomFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
        context .addEventListener( new ContextLoaderListener() );

        // the problematic line
        restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");

       // ---
   }
}

The problem is I moved all the configuration from XML to annotated class, and I don;t know of any way how to point the initParameter to my annotated class which contains all of the annotations such as SpringBootApplication, ComponentScan, etc...

So I was thinking, I will move this (and bunch of other stuff) to implemented and overriden method from ServletContextInitializer void onStartup(ServletContext servletContext) but the javax.servlet.ServletContext argument is not quite the same as org.eclipse.jetty.servlet.ServletContextHandler which offers me more options like setting SessionHandler, ResourceHandler, ContextHandler... (I was able to set some of the values to the ServletContext but not all...)

Any help that'd point me to right direction is appreciated...

1 Answers

Soo I figured it out... for XML based configuration you set just this:

restContext.setInitParameter("contextConfigLocation", "classpath:application-context.xml");

For class based you must set these two:

    restContext.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    restContext.setInitParameter("contextConfigLocation", Main.class.getName());
Related