user2316667
user2316667

Reputation: 5634

Error when using JAX-RS and Jetty?

I'm trying to create a simple REST api and I followed the tutorial on Jersey's website: https://jersey.java.net/nonav/documentation/2.0/deployment.html

Here is the error:

Caused by: javax.servlet.UnavailableException: No class in holder at org.eclipse.jetty.servlet.BaseHolder.doStart(BaseHolder.java:88) at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:361) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:874)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <display-name>My JAX-RS Servlet</display-name>
        <servlet-name>MyJaxRsApp</servlet-name>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.flexible.helloworld.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyJaxRsApp</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

REST resource class

package com.example.flexible.helloworld;

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

public class MyJaxRsApplication {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Path("helloworld")
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

Application "root"

package com.example.flexible.helloworld;

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

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(MyJaxRsApplication.class);
        return s;
    }
}

pom.xml

<!-- [START project] -->
<project>
  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <groupId>org.myproject</groupId>
  <artifactId>myproject-0.0</artifactId>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>

    <appengine.maven.plugin>0.1.1-beta</appengine.maven.plugin>
    <jetty-maven-plugin-version>9.3.7.v20160115</jetty-maven-plugin-version>

    <failOnMissingWebXml>false</failOnMissingWebXml>
  </properties>

  <!-- [START dependencies] -->
  <dependencies>

    <!-- REST framework -->
    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-server</artifactId>
      <version>2.23.2</version>
    </dependency>

  </dependencies>
  <!-- [END dependencies] -->

  <build>
    <!-- for hot reload of the web application -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>

      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${appengine.maven.plugin}</version>
        <configuration>
          <!-- deploy configuration -->
          <deploy.promote>true</deploy.promote>
          <deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty-maven-plugin-version}</version>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>

    </plugins>
  </build>
</project>
<!-- [END project] -->

I actually built this ontop of Google AppEngine helloworld example that uses jetty so the way I deploy it is 'mvn clean jetty:run-exploded'. The way I understand it, jetty is a web-server/servlet container. But do I have a servlet here? Is one created implicitly?

Also, I looked up the error and saw <servlet-class> needs to be defined. This is mentioned in jersey's documentation as well. But when I add the line <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> I get Resource not found at localhost:8080/helloworld. Also, servlet 3.0 shouldn't require <servlet-class> to be defined (also mentioned in jersey's documentation).

Upvotes: 1

Views: 1129

Answers (1)

pitaside
pitaside

Reputation: 678

Setting JAX-RS application configuration without web.xml

Using the ResourceConfig (no need for web.xml)

@javax.ws.rs.ApplicationPath(ResourcePath.API_ROOT)
    public class ApplicationConfig extends ResourceConfig {

     public ApplicationConfig() {
    //register the necessary headers files needed from client
    register(CORSConfigurationFilter.class);
    //The jackson feature and provider is used for object serialization

    register(JacksonFeature.class);
    register(JacksonProvider.class);
    //inject and registered all resources class using the package

    packages("com.example.myapi.package");
  }

  @Override
  public Collection<String> getPropertyNames() {
    return super.getPropertyNames();
  }
}

Upvotes: 1

Related Questions