Source code file content

Revision: 5845

JERSEY-1726: updated signature of Jersey TestContainerFactory supports method + copyright update
» Project Revision History

» Checkout URL

svn / trunk / jersey / jersey-documentation / src / docbook / getting-started.xml

Size: 12989 bytes, 1 line
<?xml version="1.0"?>


    Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright


<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
<!ENTITY % ents SYSTEM "jersey.ent" >

<chapter id="getting-started">
  <title>Getting Started</title>

  <para>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.</para>
<para>First, it is necessary to depend on the correct Jersey artifacts
as described in <xref linkend="chapter_deps"/>
<para>Maven developers require a dependency on

   <listitem>the <ulink url=";&amp;g=com.sun.jersey&amp;a=jersey-server&amp;v=&version;&amp;e=pom">jersey-server</ulink> module, </listitem>
   <listitem>the <ulink url=";&amp;g=com.sun.jersey&amp;a=jersey-grizzly2&amp;v=&version;&amp;e=pom">jersey-grizzly2</ulink> module, </listitem>
   The following dependencies need to be added to the pom:
<programlisting language="xml">&lt;dependency&gt;

    <para>If you want to depend on Jersey snapshot versions the following repository needs to be added to the pom:
    <programlisting language="xml">&lt;repository&gt;
    &lt;name&gt; Snapshot Repository for Maven&lt;/name&gt;

Non-maven developers require:
<listitem><ulink url=";/grizzly-http-server-&grizzly2.version;.jar">grizzly-http-server.jar</ulink>,</listitem>
<listitem><ulink url=";/grizzly-http-&grizzly2.version;.jar">grizzly-http.jar</ulink>,</listitem>
<listitem><ulink url=";/grizzly-framework-&grizzly2.version;.jar">grizzly-framework.jar</ulink>,</listitem>
<listitem><ulink url=";&amp;g=com.sun.jersey&amp;a=jersey-server&amp;v=&version;&amp;e=jar">jersey-server.jar</ulink>,</listitem>
<listitem><ulink url=";&amp;g=com.sun.jersey&amp;a=jersey-grizzly2&amp;v=&version;&amp;e=jar">jersey-grizzly2.jar</ulink>,</listitem>
<listitem><ulink url=";&amp;g=com.sun.jersey&amp;a=jersey-core&amp;v=&version;&amp;e=jar">jersey-core.jar</ulink>,</listitem>
<listitem><ulink url=";/jsr311-api-&jsr311-api-jar.version;.jar">jsr311-api.jar</ulink>,</listitem>
<listitem><ulink url=";/asm-&asm.version;.jar">asm.jar</ulink></listitem>
<para>For Ant
developers the <ulink url="">Ant
Tasks for Maven</ulink> may be used to add the following to the ant
document such that the dependencies do not need to be downloaded
<programlisting language="xml">&lt;artifact:dependencies pathId=&quot;dependency.classpath&quot;&gt;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
  &lt;dependency groupId=&quot;javax/ws/rs&quot;
  &lt;dependency groupId=&quot;asm&quot;
  &lt;artifact:remoteRepository id=&quot;;
                             url=&quot;; /&gt;
  &lt;artifact:remoteRepository id=&quot;;
                             layout=&quot;legacy&quot; /&gt;

The path id “dependency.classpath” may then be referenced as the
classpath to be used for compiling or executing.</para>
<para>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 <xref linkend="getting.started.created"/>

<title>Creating a root resource</title>
<para>Create the following Java class in your project:</para>
<programlisting language="java" linenumbering="numbered">    // The Java class will be hosted at the URI path &quot;/helloworld&quot;
    public class HelloWorldResource {
        // The Java method will process HTTP GET requests
        // The Java method will produce content identified by the MIME Media
        // type &quot;text/plain&quot;
        public String getClichedMessage() {
            // Return some cliched textual content
            return &quot;Hello World&quot;;
The <literal>HelloWorldResource</literal> class is a very simple Web
resource. The URI path of the resource is &quot;/helloworld&quot;
(line 2), it supports the HTTP GET method (line 6) and produces
cliched textual content (line 12) of the MIME media type &quot;text/plain&quot;
(line 9).</para>
<para>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.</para>

<title>Deploying the root resource</title>
<para>The root resource will be deployed using the Grizzly Web
<para>Create the following Java class in your project:</para>
<programlisting language="java" linenumbering="numbered">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 java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Main {

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

    public static final URI BASE_URI = getBaseURI();

    protected static HttpServer startServer() throws IOException {
        System.out.println(&quot;Starting grizzly...&quot;);
        ResourceConfig rc = new PackagesResourceConfig(&quot;com.sun.jersey.samples.helloworld.resources&quot;);
        return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
    public static void main(String[] args) throws IOException {
        HttpServer httpServer = startServer();
        System.out.println(String.format(&quot;Jersey app started with WADL available at &quot;
                + &quot;%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...&quot;,
                BASE_URI, BASE_URI));;
The <literal>Main</literal> class deploys the <literal>HelloWorldResource</literal>
using the Grizzly Web container.</para>
<para>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 <literal>com.sun.jersey.samples.helloworld.resources</literal>
(or in a sub-package of).</para>
<para>Line 25 deploys the root resource to the base URI
&quot;<ulink url="http://localhost:9998/">http://localhost:9998/</ulink>&quot;
and returns a Grizzly HttpServer. The complete URI of the Hello
World root resource is &quot;<ulink url="http://localhost:9998/helloworld">http://localhost:9998/helloworld</ulink>&quot;.</para>
<para>Notice that no deployment descriptors were needed and the root
resource was setup in a few statements of Java code.</para>

<title>Testing the root resource</title>
<para>Goto the URI <ulink url="http://localhost:9998/helloworld">http://localhost:9998/helloworld</ulink>
in your favourite browser.</para>
<para>Or, from the command line use <literal>curl</literal>:
<programlisting>&gt; curl http://localhost:9998/helloworld</programlisting>

<section id="getting.started.created">
<title>Here's one Paul created earlier</title>
<para>The example code presented above is shipped as the <ulink url=";&amp;g=com.sun.jersey.samples&amp;a=helloworld&amp;v=&version;&amp;c=project&amp;e=zip">HelloWorld</ulink>
sample in the Java.Net maven repository.</para>
<para>For developers wishing to get started by deploying a Web
application an equivalent sample, as a Web application, is shipped as
the <ulink url=";&amp;g=com.sun.jersey.samples&amp;a=helloworld-webapp&amp;v=&version;&amp;c=project&amp;e=zip">HelloWorld-WebApp</ulink>

Terms of Use; Privacy Policy; Copyright ©2013-2015 (revision 20150226.965aeb8)
Please Confirm