RustyTheBoyRobot
RustyTheBoyRobot

Reputation: 5945

Jersey setup without web.xml

I'm attempting to set up a simple REST web application that uses Jersey. In the documentation, it seems that I should be able to create my application without using a web.xml file. From the site:

JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes.

The example that follows shows this code:

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

To me, this says that I can use an Application class to do all of my servlet setup. This seems to be the configuration that reads my resource class's annotations and sets up the correct URL handling mechanisms. Is that correct? I don't have to do any other setup?


I ask because I created the following and it didn't work (I get a 404 from localhost:8080/{context}/test):

pom.xml:

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.12</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.12</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.12</version>
    </dependency>   

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

Application class:

@ApplicationPath("/")
public class JerseyTestApp extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {
        final Set<Class<?>> classes = new HashSet<>();
        classes.add(JerseyTestController.class);
        return classes;
    }
}

Resource class:

@Path("/test")
public class JerseyTestController 
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTestMsg() 
    {
        return "It works";
    }
}

Upvotes: 7

Views: 8765

Answers (1)

RustyTheBoyRobot
RustyTheBoyRobot

Reputation: 5945

Dumb. All I had to do was include the jersey-servlet jar, as prescribed by this answer.

Upvotes: 10

Related Questions