AbstractMethodError when creating JdkHttpServer for JAX-RS

Tags: , , ,



I’m trying to create a Java server for a REST API and following this question I used jersey’s JdkHttpServer. I imported the necessary jars (from here), but when I start the server the following error appears:

Exception in thread "main" java.lang.AbstractMethodError: org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainerProvider.createContainer(Ljava/lang/Class;Ljavax/ws/rs/core/Application;)Ljava/lang/Object;
at org.glassfish.jersey.server.ContainerFactory.createContainer(ContainerFactory.java:58)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:78)
at PokerJAXRS.main(PokerJAXRS.java:16)

My code is the following:

import com.sun.net.httpserver.HttpServer;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.net.URI;

public class PokerJAXRS {

    private static String baseUri = "http://localhost:9998/";

    public static void main(String[] args) throws Exception {
        URI bUri = URI.create(baseUri);

        HttpServer server = JdkHttpServerFactory.createHttpServer(bUri, new ResourceConfig(PokerResource.class));

        System.out.println("Server running");
        System.out.println("Visit: http://localhost:9998/");
        System.out.println("Hit return to stop...");
        System.in.read();
        server.stop(0);
        System.out.println("Server stopped");
    }
}

And in PokerResource I’ve got all the paths for the API. Am I missing some import or do I have to define anything else?

EDIT: These are all the jar libs I’m using:

enter image description here

Answer

My first recommendation, for anyone working with Java, is to consider a dependency management approach. As I’m more used to maven, so I’ll share here what worked for me as part of a Maven pom.xml file, but you can always address it as it suits better to you:

<properties>
    <jersey.version>2.33</jersey.version>
</properties>

<dependencies>

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>${jersey.version}</version>
    </dependency>

As you can notice, it’s basically about not mixing different jersey versions. Finally, it’s a good practice to control versions by using a property whenever it fits.



Source: stackoverflow