2

I am very new to Java, I have basically just started with Java coming from PHP.

I am trying to get a RESTful API up and running with help from Jersey and Tomcat. But I seem to make a mistake and I cannot find any post or thread with the same error message as I am getting. I would be very grateful for any help regarding this issue, thanks!

This is my Maven dependencies

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-servlet</artifactId>
        <version>2.9</version>
    </dependency>

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

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-simple-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-servlet</artifactId>
        <version>2.9</version>
    </dependency>
</dependencies>

This is the web.xml

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

This is my code

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/account")
public class AccountREST
{

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String info(){
        return "This is the RESTful API";
    }
    @GET
    @Path("/getit")
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt(){
        System.out.println("Got it!");
        return "Got it!";
    }
}

When i go into the URL http://localhost:8080/rest/account/getit I get this error message which I cannot decipher

java.lang.NullPointerException
    org.apache.catalina.loader.WebappClassLoader.binaryNameToPath(WebappClassLoader.java:2496)
    org.apache.catalina.loader.WebappClassLoader.findLoadedClass0(WebappClassLoader.java:2702)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1223)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1175)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2451)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2440)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)
Kansuler
  • 1,539
  • 2
  • 11
  • 17
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – arserbin3 Jun 01 '14 at 05:49
  • I eventually got it to work, after hours of struggling. The thread you sent is indirectly related to my issues. It was caused by me not implementing Jersey's API properly. So the issue was triggered within the external libraries. I will send an answer to this thread, explaining what I did for others to see once I can. – Kansuler Jun 01 '14 at 07:10
  • glad to hear it, and hopefully that explanation will be helpful to someone in the future. – arserbin3 Jun 01 '14 at 14:12

1 Answers1

2

I eventually got it to work, I have been struggling with it for many hours now. The article you posted above is somewhat related to my problem, but in one of the Jersey API's. I hadn't implemented Jersey properly. I found all the guides and tutorials to just partially explain how to implement Jersey RESTful API, which made me quite confused.

For anyone who meet the same issues as I had, here is my new setup:

pom.xml

<dependencies>
    <!-- Servlet 3.0 API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- JAX-RS RESTful Web Services -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.9</version>
    </dependency>
</dependencies>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

</web-app>

RESTConfig.class

import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/rest")
public class RESTConfig extends Application
{

    public RESTConfig(){}

    @Override
    public Set<Class<?>> getClasses( )
    {
        final Set<Class<?>> returnValue = new HashSet<Class<?>>( );
        returnValue.add( Account.class );
        return returnValue;
    }
}

Account.class

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/account")
public class Account
{

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String info(){
        return "This is the Account RESTful API";
    }

    @GET
    @Path("/getit")
    @Produces(MediaType.TEXT_PLAIN)
    public String getit(){
        System.out.println("Got it!");
        return "Got it!";
    }

}
Kansuler
  • 1,539
  • 2
  • 11
  • 17
  • 2
    Thank you! Jersey documentation is suxx. Noticed one typo in RestConfig in the place where you add classes to HashMap. It should be Account, not AccountREST – x4444 May 01 '15 at 23:23