Sophie

Sophie

distrib > Fedora > 18 > x86_64 > media > updates > by-pkgid > cc4dd867882cf37d3c55e57c589f409d > files > 7

jersey-1.17.1-2.fc18.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
	<TITLE>Getting Started</TITLE>
	<META NAME="GENERATOR" CONTENT="OpenOffice.org 3.0 Beta (Unix)">
	<META NAME="CREATED" CONTENT="0;0">
	<META NAME="CHANGED" CONTENT="20081008;15594200">
	<META NAME="Info 1" CONTENT="">
	<META NAME="Info 2" CONTENT="">
	<META NAME="Info 3" CONTENT="">
	<META NAME="Info 4" CONTENT="">
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<H1>Getting Started</H1>
<P>This document will present how to get started with Jersey using
the embedded Grizzly server. The last section of this document
presents a reference to equivalent functionality for getting started
with a Web application.</P>
<P>First, it is necessary to depend on the correct Jersey artifacts
as described in the <A HREF="dependencies.html">dependencies</A>
document.</P>
<P>Maven developers require a dependency on the <A HREF="http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.1.3-ea-SNAPSHOT/jersey-server-1.1.3-ea-SNAPSHOT.pom">jersey-server</A>
module, the <A HREF="http://download.java.net/maven/2/com/sun/grizzly/grizzly-servlet-webserver/1.9.8/grizzly-servlet-webserver-1.9.8.pom">grizzly-servlet-webserver</A>
module and optionally for WADL support if using Java SE 5 the
<A HREF="http://download.java.net/maven/1/com.sun.xml.bind/poms/jaxb-impl-2.1.pom">jaxb-impl</A>
module. The following dependencies need to be added to the pom:</P>
<PRE STYLE="margin-left: 0.79in">&lt;dependency&gt;
    &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
    &lt;version&gt;1.1.3-ea-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;&lt;dependency&gt;
    &lt;groupId&gt;com.sun.grizzly&lt;/groupId&gt;
    &lt;artifactId&gt;grizzly-servlet-webserver&lt;/artifactId&gt;
    &lt;version&gt;1.9.8&lt;/version&gt;
&lt;/dependency&gt;</PRE><P>
And the following repositories need to be added to the pom:</P>
<PRE STYLE="margin-left: 0.79in">&lt;repository&gt;
    &lt;id&gt;maven2-repository.dev.java.net&lt;/id&gt;
    &lt;name&gt;Java.net Repository for Maven&lt;/name&gt;
    &lt;url&gt;http://download.java.net/maven/2/&lt;/url&gt;
    &lt;layout&gt;default&lt;/layout&gt;
&lt;/repository&gt; 
&lt;repository&gt;
    &lt;id&gt;maven-repository.dev.java.net&lt;/id&gt;
    &lt;name&gt;Java.net Maven 1 Repository (legacy)&lt;/name&gt;
    &lt;url&gt;http://download.java.net/maven/1&lt;/url&gt;
    &lt;layout&gt;legacy&lt;/layout&gt;
&lt;/repository&gt;</PRE><P>
Non-maven developers require:</P>
<BLOCKQUOTE><A HREF="http://download.java.net/maven/2/com/sun/grizzly/grizzly-servlet-webserver/1.9.8/grizzly-servlet-webserver-1.9.8.jar">grizzly-servlet-webserver.jar</A>,
<A HREF="http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.1.3-ea-SNAPSHOT/jersey-server-1.1.3-ea-SNAPSHOT.jar">jersey-server.jar</A>,
<A HREF="http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.1.3-ea-SNAPSHOT/jersey-core-1.1.3-ea-SNAPSHOT.jar">jersey-core.jar</A>,
<A HREF="http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar">jsr311-api.jar</A>,
<A HREF="http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar">asm.jar</A></BLOCKQUOTE>
<P>and optionally for WADL support if using Java SE 5:</P>
<BLOCKQUOTE><A HREF="http://download.java.net/maven/1/com.sun.xml.bind/jars/jaxb-impl-2.1.jar">jaxb-impl.jar</A>,
<A HREF="http://download.java.net/maven/1/javax.xml.bind/jars/jaxb-api-2.1.jar">jaxb-api.jar</A>,
<A HREF="http://download.java.net/maven/1/javax.activation/jars/activation-1.1.jar">activation.jar</A>,
<A HREF="http://download.java.net/maven/1/javax.xml.stream/jars/stax-api-1.0-2.jar">stax-api.jar</A></BLOCKQUOTE>
<BLOCKQUOTE STYLE="margin-left: 0in; margin-right: 0in">For Ant
developers the <A HREF="http://maven.apache.org/ant-tasks.html">Ant
Tasks for Maven</A> may be used to add the following to the ant
document such that the dependencies do not need to be downloaded
explicitly:</BLOCKQUOTE>
<PRE STYLE="margin-left: 0.79in">&lt;artifact:dependencies pathId=&quot;dependency.classpath&quot;&gt;
  &lt;dependency groupId=&quot;com.sun.jersey&quot; 
              artifactId=&quot;jersey-server&quot;
              version=&quot;1.1.3-ea-SNAPSHOT&quot;/&gt;
  &lt;dependency groupId=&quot;com.sun.grizzly&quot; 
              artifactId=&quot;grizzly-servlet-webserver&quot;
              version=&quot;1.8.6.4&quot;/&gt;
  &lt;artifact:remoteRepository id=&quot;maven2-repository.dev.java.net&quot;
                             url=&quot;http://download.java.net/maven/2/&quot; /&gt;
  &lt;artifact:remoteRepository id=&quot;maven-repository.dev.java.net&quot;
                             url=&quot;http://download.java.net/maven/1&quot; 
                             layout=&quot;legacy&quot; /&gt;
&lt;/artifact:dependencies&gt;</PRE><BLOCKQUOTE STYLE="margin-left: 0in; margin-right: 0in">
The path id “dependency.classpath” may then be referenced as the
classpath to be used for compiling or executing.</BLOCKQUOTE>
<P>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 the last <A HREF="#created">section</A>
of this document.)</P>
<H1>Creating a root resource</H1>
<P>Create the following Java class in your project:</P>
<PRE> 1    // The Java class will be hosted at the URI path &quot;/helloworld&quot;
 2    @Path(&quot;/helloworld&quot;)
 3    public class HelloWorldResource {
 4    
 5        // The Java method will process HTTP GET requests
 6        @GET 
 7        // The Java method will produce content identified by the MIME Media
 8        // type &quot;text/plain&quot;
 9        @Produces(&quot;text/plain&quot;)
10        public String getClichedMessage() {
11            // Return some cliched textual content
12            return &quot;Hello World&quot;;
13        }
14    }</PRE><P>
The <CODE>HelloWorldResource</CODE> 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).</P>
<P>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.</P>
<H1>Deploying the root resource</H1>
<P>The root resource will be deployed using the Grizzly Web
container.</P>
<P>Create the following Java class in your project:</P>
<PRE> 1    public class Main {
 2         
 3        public static void main(String[] args) throws IOException {
 4            
 5            final String baseUri = &quot;http://localhost:9998/&quot;;
 6            final Map&lt;String, String&gt; initParams = new HashMap&lt;String, String&gt;();
 7
 8            initParams.put(&quot;com.sun.jersey.config.property.packages&quot;, 
 9                    &quot;com.sun.jersey.samples.helloworld.resources&quot;);
10
11            System.out.println(&quot;Starting grizzly...&quot;);
12            SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
13                    baseUri, initParams);
14            System.out.println(String.format(
15                    &quot;Jersey app started with WADL available at %sapplication.wadl\n” + 
16                    “Try out %shelloworld\nHit enter to stop it...&quot;, baseUri, baseUri));
17            System.in.read();
18            threadSelector.stopEndpoint();
19            System.exit(0);
20        }    
21    }</PRE><P>
The <CODE>Main</CODE> class deploys the <CODE>HelloWorldResource</CODE>
using the Grizzly Web container.</P>
<P>Lines 8 to 9 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 <FONT FACE="Courier New, monospace">com.sun.jersey.samples.helloworld.resources</FONT>
(or in a sub-package of).</P>
<P>Lines 12 to 13 deploys the root resource to the base URI
&quot;<A HREF="http://localhost:9998/">http://localhost:9998/</A>&quot;
and returns a Grizzly SelectorThread. The complete URI of the Hello
World root resource is &quot;<A HREF="http://localhost:9998/helloworld">http://localhost:9998/helloworld</A>&quot;.</P>
<P>Notice that no deployment descriptors were needed and the root
resource was setup in a few statements of Java code.</P>
<H1>Testing the root resource</H1>
<P>Goto the URI <A HREF="http://localhost:9998/helloworld">http://localhost:9998/helloworld</A>
in your favourite browser.</P>
<P>Or, from the command line use <CODE>curl:</CODE></P>
<PRE STYLE="margin-bottom: 0.2in"><CODE>    &gt; curl http://localhost:9998/helloworld</CODE></PRE><H1>
<A NAME="created"></A>Here's one I created earlier</H1>
<P>The example code presented above is shipped as the <A HREF="http://download.java.net/maven/2/com/sun/jersey/samples/helloworld/1.1.3-ea-SNAPSHOT/helloworld-1.1.3-ea-SNAPSHOT-project.zip">HelloWorld</A>
sample in the Java.Net maven repository.</P>
<P>For developers wishing to get started by deploying a Web
application an equivalent sample, as a Web application, is shipped as
the <A HREF="http://download.java.net/maven/2/com/sun/jersey/samples/helloworld-webapp/1.1.3-ea-SNAPSHOT/helloworld-webapp-1.1.3-ea-SNAPSHOT-project.zip">HelloWorld-WebApp</A>
sample.</P>
</BODY>
</HTML>