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"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    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
    http://glassfish.java.net/public/CDDL+GPL_1_1.html
    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.

    Modifications:
    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]"

    Contributor(s):
    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
    holder.

-->

<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
                 "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"[
<!ENTITY % ents SYSTEM "jersey.ent" >
%ents;
]>

<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>
<para>Maven developers require a dependency on

<itemizedlist>
   <listitem>the <ulink url="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey&amp;a=jersey-server&amp;v=&version;&amp;e=pom">jersey-server</ulink> module, </listitem>
   <listitem>the <ulink url="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey&amp;a=jersey-grizzly2&amp;v=&version;&amp;e=pom">jersey-grizzly2</ulink> module, </listitem>
  </itemizedlist>
 </para>
 <para>
   The following dependencies need to be added to the pom:
<programlisting language="xml">&lt;dependency&gt;
    &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
    &lt;version&gt;&version;&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-grizzly2&lt;/artifactId&gt;
    &lt;version&gt;&version;&lt;/version&gt;
&lt;/dependency&gt;</programlisting></para>

    <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;id&gt;snapshot-repository.java.net&lt;/id&gt;
    &lt;name&gt;Java.net Snapshot Repository for Maven&lt;/name&gt;
    &lt;url&gt;https://maven.java.net/content/repositories/snapshots/&lt;/url&gt;
    &lt;layout&gt;default&lt;/layout&gt;
&lt;/repository&gt;</programlisting></para>

<para>
Non-maven developers require:
<itemizedlist>
<listitem><ulink url="http://search.maven.org/remotecontent?filepath=org/glassfish/grizzly/grizzly-http-server/&grizzly2.version;/grizzly-http-server-&grizzly2.version;.jar">grizzly-http-server.jar</ulink>,</listitem>
<listitem><ulink url="http://search.maven.org/remotecontent?filepath=org/glassfish/grizzly/grizzly-http/&grizzly2.version;/grizzly-http-&grizzly2.version;.jar">grizzly-http.jar</ulink>,</listitem>
<listitem><ulink url="http://search.maven.org/remotecontent?filepath=org/glassfish/grizzly/grizzly-framework/&grizzly2.version;/grizzly-framework-&grizzly2.version;.jar">grizzly-framework.jar</ulink>,</listitem>
<listitem><ulink url="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey&amp;a=jersey-server&amp;v=&version;&amp;e=jar">jersey-server.jar</ulink>,</listitem>
<listitem><ulink url="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey&amp;a=jersey-grizzly2&amp;v=&version;&amp;e=jar">jersey-grizzly2.jar</ulink>,</listitem>
<listitem><ulink url="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey&amp;a=jersey-core&amp;v=&version;&amp;e=jar">jersey-core.jar</ulink>,</listitem>
<listitem><ulink url="http://search.maven.org/remotecontent?filepath=javax/ws/rs/jsr311-api/&jsr311-api-jar.version;/jsr311-api-&jsr311-api-jar.version;.jar">jsr311-api.jar</ulink>,</listitem>
<listitem><ulink url="http://search.maven.org/remotecontent?filepath=asm/asm/&asm.version;/asm-&asm.version;.jar">asm.jar</ulink></listitem>
</itemizedlist></para>
<para>For Ant
developers the <ulink url="http://maven.apache.org/ant-tasks/index.html">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
explicitly:</para>
<programlisting language="xml">&lt;artifact:dependencies pathId=&quot;dependency.classpath&quot;&gt;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
              artifactId=&quot;jersey-server&quot;
              version=&quot;&version;&quot;/&gt;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
              artifactId=&quot;jersey-core&quot;
              version=&quot;&version;&quot;/&gt;
  &lt;dependency groupId=&quot;com.sun.jersey&quot;
              artifactId=&quot;jersey-grizzly2&quot;
              version=&quot;&version;&quot;/&gt;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
              artifactId=&quot;grizzly-http-server&quot;
              version=&quot;&grizzly2.version;&quot;/&gt;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
              artifactId=&quot;grizzly-http&quot;
              version=&quot;&grizzly2.version;&quot;/&gt;
  &lt;dependency groupId=&quot;org.glassfish.grizzly&quot;
              artifactId=&quot;grizzly-framework&quot;
              version=&quot;&grizzly2.version;&quot;/&gt;
  &lt;dependency groupId=&quot;javax/ws/rs&quot;
              artifactId=&quot;jsr311-api&quot;
              version=&quot;&jsr311-api-jar.version;&quot;/&gt;
  &lt;dependency groupId=&quot;asm&quot;
              artifactId=&quot;asm&quot;
              version=&quot;&asm.version;&quot;/&gt;
  &lt;artifact:remoteRepository id=&quot;maven-repository.java.net&quot;
                             url=&quot;http://maven.java.net/&quot; /&gt;
  &lt;artifact:remoteRepository id=&quot;maven1-repository.java.net&quot;
                             url=&quot;http://download.java.net/maven/1&quot;
                             layout=&quot;legacy&quot; /&gt;
&lt;/artifact:dependencies&gt;</programlisting>

<para>
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"/>
</para>

<section>
<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;
    @Path(&quot;/helloworld&quot;)
    public class HelloWorldResource {
    
        // The Java method will process HTTP GET requests
        @GET 
        // The Java method will produce content identified by the MIME Media
        // type &quot;text/plain&quot;
        @Produces(&quot;text/plain&quot;)
        public String getClichedMessage() {
            // Return some cliched textual content
            return &quot;Hello World&quot;;
        }
    }</programlisting><para>
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>
</section>

<section>
<title>Deploying the root resource</title>
<para>The root resource will be deployed using the Grizzly Web
container.</para>
<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 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(&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));
        System.in.read();
        httpServer.stop();
    }    
}</programlisting><para>
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>
</section>

<section>
<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>
</para>
</section>

<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="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&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="https://maven.java.net/service/local/artifact/maven/redirect?r=&repository;&amp;g=com.sun.jersey.samples&amp;a=helloworld-webapp&amp;v=&version;&amp;c=project&amp;e=zip">HelloWorld-WebApp</ulink>
sample.</para>
</section>

</chapter>
Terms of Use; Privacy Policy; Copyright ©2013-2015 (revision 20150226.965aeb8)
 
 
Close
loading
Please Confirm
Close