How to register AnnotationConfigWebApplicationContext in JettyHttpContainerFactory?

Viewed 67

I am trying to integrate the Spring DI container using the jersey-container-jetty-http dependency. Here is my code:

var applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(ApplicationConfig.class);

        final ResourceConfig config = new ResourceConfig()
                .packages("resources")
                .property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true)
                .register(new ContextLoaderListener(applicationContext));

        // Start Jetty Server
        return JettyHttpContainerFactory.createServer(
                        URI.create(String.format("http://localhost:%d/", port)), config);

Using this code I get java.lang.ClassNotFoundException: javax.servlet.ServletContextListener.

Some tutorials say that I need to addEventListener via SevletContextHandler like:

JettyHttpContainerFactory.createServer(
                        URI.create(String.format("http://localhost:%d/", port)), config)
                  .addEventListener(new ContextLoaderListener(applicationContext));

But addEventListener requires EventListener type.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.ev</groupId>
  <artifactId>jaxrsbasic</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>jaxrsbasic</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>6.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>com.opentable.components</groupId>
      <artifactId>otj-pg-embedded</artifactId>
      <version>1.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-bean-validation</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-jetty-http</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-spring5</artifactId>
      <version>3.0.0-M6</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>11.0.9</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>jaxrsbasic</finalName>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution><goals><goal>java</goal></goals></execution>
        </executions>
        <configuration><mainClass>server.JettyServer</mainClass></configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>16</source>
          <target>16</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

1 Answers

Do you want to use JDK 1.8 or JDK 16 ?

Because I see in you pom.xml :

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

you are pointing to JDK 1.8.

but afterwards you are using JDK 16 in your pom.xml for maven-compiler-plugin :

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>16</source>
          <target>16</target>
        </configuration>
      </plugin>

This is incorrect. You should avoid keeping two different versions of JDK for compilation. You should use any one of them.

Secondly, you are using jakarta-servlet-api-5.0.0 dependency in pom.xml which I suspect having no class calledjavax.servlet.ServletContextListener. Better decompile the jars coming from that dependency and check for that class.

To fix that exception, you can use javax.servlet-api-4.0.1 which has this class javax.servlet.ServletContextListener :

       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
  • Follow this article here for a proper approach on registering AnnotationConfigWebApplicationContext.
Related