# Getting Started

This chapter will present how to get started with Jersey using the embedded Grizzly server. The last section of this chapter presents a reference to equivalent functionality for getting started with a Web application.

First, it is necessary to depend on the correct Jersey artifacts as described in

Maven developers require a dependency on

- the jersey-server module,
- the jersey-grizzly2 module,

The following dependencies need to be added to the pom:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>&version;</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly2</artifactId>
        <version>&version;</version>
    </dependency>

If you want to depend on Jersey snapshot versions the following repository needs to be added to the pom:

    <repository>
        <id>snapshot-repository.java.net</id>
        <name>Java.net Snapshot Repository for Maven</name>
        <url>https://maven.java.net/content/repositories/snapshots/</url>
        <layout>default</layout>
    </repository>

Non-maven developers require:

- grizzly-http-server.jar,
- grizzly-http.jar,
- grizzly-framework.jar,
- jersey-server.jar,
- jersey-grizzly2.jar,
- jersey-core.jar,
- jsr311-api.jar,

For Ant developers the Ant Tasks for Maven may be used to add the following to the ant document such that the dependencies do not need to be downloaded explicitly:

    <artifact:dependencies pathId="dependency.classpath">
      <dependency groupId="com.sun.jersey"
                  artifactId="jersey-server"
                  version="&version;"/>
      <dependency groupId="com.sun.jersey"
                  artifactId="jersey-core"
                  version="&version;"/>
      <dependency groupId="com.sun.jersey"
                  artifactId="jersey-grizzly2"
                  version="&version;"/>
      <dependency groupId="org.glassfish.grizzly"
                  artifactId="grizzly-http-server"
                  version="&grizzly2.version;"/>
      <dependency groupId="org.glassfish.grizzly"
                  artifactId="grizzly-http"
                  version="&grizzly2.version;"/>
      <dependency groupId="org.glassfish.grizzly"
                  artifactId="grizzly-framework"
                  version="&grizzly2.version;"/>
      <dependency groupId="javax/ws/rs"
                  artifactId="jsr311-api"
                  version="&jsr311-api-jar.version;"/>
      <artifact:remoteRepository id="maven-repository.java.net"
                                 url="http://maven.java.net/" />
      <artifact:remoteRepository id="maven1-repository.java.net"
                                 url="http://download.java.net/maven/1"
                                 layout="legacy" />
    </artifact:dependencies>

The path id “dependency.classpath” may then be referenced as the classpath to be used for compiling or executing.

Second, create a new project (using your favourite IDE or just ant/maven) and add the dependences. (For those who want to skip the creation of their own project take a look at

## Creating a root resource

Create the following Java class in your project:

        // The Java class will be hosted at the URI path "/helloworld"
        @Path("/helloworld")
        public class HelloWorldResource {

            // The Java method will process HTTP GET requests
            @GET
            // The Java method will produce content identified by the MIME Media
            // type "text/plain"
            @Produces("text/plain")
            public String getClichedMessage() {
                // Return some cliched textual content
                return "Hello World";
            }
        }

The `HelloWorldResource` class is a very simple Web resource. The URI path of the resource is "/helloworld" (line 2), it supports the HTTP GET method (line 6) and produces cliched textual content (line 12) of the MIME media type "text/plain" (line 9).

Notice the use of Java annotations to declare the URI path, the HTTP method and the media type. This is a key feature of JSR 311.

## Deploying the root resource

The root resource will be deployed using the Grizzly Web container.

Create the following Java class in your project:

    import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
    import com.sun.jersey.api.core.DefaultResourceConfig;
    import com.sun.jersey.api.core.PackagesResourceConfig;
    import com.sun.jersey.api.core.ResourceConfig;
    import org.glassfish.grizzly.http.server.HttpServer;

    import javax.ws.rs.core.UriBuilder;
    import java.io.IOException;
    import java.net.URI;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;

    public class Main {

        private static URI getBaseURI() {
            return UriBuilder.fromUri("http://localhost/").port(9998).build();
        }

        public static final URI BASE_URI = getBaseURI();

        protected static HttpServer startServer() throws IOException {
            System.out.println("Starting grizzly...");
            ResourceConfig rc = new PackagesResourceConfig("com.sun.jersey.samples.helloworld.resources");
            return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
        }

        public static void main(String[] args) throws IOException {
            HttpServer httpServer = startServer();
            System.out.println(String.format("Jersey app started with WADL available at "
                    + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
                    BASE_URI, BASE_URI));
            System.in.read();
            httpServer.stop();
        }
    }

The `Main` class deploys the `HelloWorldResource` using the Grizzly Web container.

Line 24 creates an initialization parameter that informs the Jersey runtime where to search for root resource classes to be deployed. In this case it assumes the root resource class in the package `com.sun.jersey.samples.helloworld.resources` (or in a sub-package of).

Line 25 deploys the root resource to the base URI "http://localhost:9998/" and returns a Grizzly HttpServer. The complete URI of the Hello World root resource is "http://localhost:9998/helloworld".

Notice that no deployment descriptors were needed and the root resource was setup in a few statements of Java code.

## Testing the root resource

Goto the URI http://localhost:9998/helloworld in your favourite browser.

Or, from the command line use `curl`:

    > curl http://localhost:9998/helloworld

## Here's one Paul created earlier

The example code presented above is shipped as the HelloWorld sample in the Java.Net maven repository.

For developers wishing to get started by deploying a Web application an equivalent sample, as a Web application, is shipped as the HelloWorld-WebApp sample.