1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
+ < HTML >
3
+ < HEAD >
4
+ < META HTTP-EQUIV ="CONTENT-TYPE " CONTENT ="text/html; charset=utf-8 ">
5
+ < TITLE > Getting Started</ TITLE >
6
+ < META NAME ="GENERATOR " CONTENT ="OpenOffice.org 3.0 Beta (Unix) ">
7
+ < META NAME ="CREATED " CONTENT ="0;0 ">
8
+ < META NAME ="CHANGED " CONTENT ="20081008;15594200 ">
9
+ < META NAME ="Info 1 " CONTENT ="">
10
+ < META NAME ="Info 2 " CONTENT ="">
11
+ < META NAME ="Info 3 " CONTENT ="">
12
+ < META NAME ="Info 4 " CONTENT ="">
13
+ </ HEAD >
14
+ < BODY LANG ="en-US " DIR ="LTR ">
15
+ < H1 > Getting Started</ H1 >
16
+ < P > This document will present how to get started with Jersey using
17
+ the embedded Grizzly server. The last section of this document
18
+ presents a reference to equivalent functionality for getting started
19
+ with a Web application.</ P >
20
+ < P > First, it is necessary to depend on the correct Jersey artifacts
21
+ as described in the < A HREF ="dependencies.html "> dependencies</ A >
22
+ document.</ P >
23
+ < 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 >
24
+ 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 >
25
+ module and optionally for WADL support if using Java SE 5 the
26
+ < A HREF ="http://download.java.net/maven/1/com.sun.xml.bind/poms/jaxb-impl-2.1.pom "> jaxb-impl</ A >
27
+ module. The following dependencies need to be added to the pom:</ P >
28
+ < PRE STYLE ="margin-left: 0.79in "> <dependency>
29
+ <groupId>com.sun.jersey</groupId>
30
+ <artifactId>jersey-server</artifactId>
31
+ <version>1.1.3-ea-SNAPSHOT</version>
32
+ </dependency><dependency>
33
+ <groupId>com.sun.grizzly</groupId>
34
+ <artifactId>grizzly-servlet-webserver</artifactId>
35
+ <version>1.9.8</version>
36
+ </dependency></ PRE > < P >
37
+ And the following repositories need to be added to the pom:</ P >
38
+ < PRE STYLE ="margin-left: 0.79in "> <repository>
39
+ <id>maven2-repository.dev.java.net</id>
40
+ <name>Java.net Repository for Maven</name>
41
+ <url>http://download.java.net/maven/2/</url>
42
+ <layout>default</layout>
43
+ </repository>
44
+ <repository>
45
+ <id>maven-repository.dev.java.net</id>
46
+ <name>Java.net Maven 1 Repository (legacy)</name>
47
+ <url>http://download.java.net/maven/1</url>
48
+ <layout>legacy</layout>
49
+ </repository></ PRE > < P >
50
+ Non-maven developers require:</ P >
51
+ < 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 > ,
52
+ < 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 > ,
53
+ < 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 > ,
54
+ < A HREF ="http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar "> jsr311-api.jar</ A > ,
55
+ < A HREF ="http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar "> asm.jar</ A > </ BLOCKQUOTE >
56
+ < P > and optionally for WADL support if using Java SE 5:</ P >
57
+ < BLOCKQUOTE > < A HREF ="http://download.java.net/maven/1/com.sun.xml.bind/jars/jaxb-impl-2.1.jar "> jaxb-impl.jar</ A > ,
58
+ < A HREF ="http://download.java.net/maven/1/javax.xml.bind/jars/jaxb-api-2.1.jar "> jaxb-api.jar</ A > ,
59
+ < A HREF ="http://download.java.net/maven/1/javax.activation/jars/activation-1.1.jar "> activation.jar</ A > ,
60
+ < A HREF ="http://download.java.net/maven/1/javax.xml.stream/jars/stax-api-1.0-2.jar "> stax-api.jar</ A > </ BLOCKQUOTE >
61
+ < BLOCKQUOTE STYLE ="margin-left: 0in; margin-right: 0in "> For Ant
62
+ developers the < A HREF ="http://maven.apache.org/ant-tasks.html "> Ant
63
+ Tasks for Maven</ A > may be used to add the following to the ant
64
+ document such that the dependencies do not need to be downloaded
65
+ explicitly:</ BLOCKQUOTE >
66
+ < PRE STYLE ="margin-left: 0.79in "> <artifact:dependencies pathId="dependency.classpath">
67
+ <dependency groupId="com.sun.jersey"
68
+ artifactId="jersey-server"
69
+ version="1.1.3-ea-SNAPSHOT"/>
70
+ <dependency groupId="com.sun.grizzly"
71
+ artifactId="grizzly-servlet-webserver"
72
+ version="1.8.6.4"/>
73
+ <artifact:remoteRepository id="maven2-repository.dev.java.net"
74
+ url="http://download.java.net/maven/2/" />
75
+ <artifact:remoteRepository id="maven-repository.dev.java.net"
76
+ url="http://download.java.net/maven/1"
77
+ layout="legacy" />
78
+ </artifact:dependencies></ PRE > < BLOCKQUOTE STYLE ="margin-left: 0in; margin-right: 0in ">
79
+ The path id “dependency.classpath” may then be referenced as the
80
+ classpath to be used for compiling or executing.</ BLOCKQUOTE >
81
+ < P > Second, create a new project (using your favourite IDE or just
82
+ ant/maven) and add the dependences. (For those who want to skip the
83
+ creation of their own project take a look at the last < A HREF ="#created "> section</ A >
84
+ of this document.)</ P >
85
+ < H1 > Creating a root resource</ H1 >
86
+ < P > Create the following Java class in your project:</ P >
87
+ < PRE > 1 // The Java class will be hosted at the URI path "/helloworld"
88
+ 2 @Path("/helloworld")
89
+ 3 public class HelloWorldResource {
90
+ 4
91
+ 5 // The Java method will process HTTP GET requests
92
+ 6 @GET
93
+ 7 // The Java method will produce content identified by the MIME Media
94
+ 8 // type "text/plain"
95
+ 9 @Produces("text/plain")
96
+ 10 public String getClichedMessage() {
97
+ 11 // Return some cliched textual content
98
+ 12 return "Hello World";
99
+ 13 }
100
+ 14 }</ PRE > < P >
101
+ The < CODE > HelloWorldResource</ CODE > class is a very simple Web
102
+ resource. The URI path of the resource is "/helloworld"
103
+ (line 2), it supports the HTTP GET method (line 6) and produces
104
+ cliched textual content (line 12) of the MIME media type "text/plain"
105
+ (line 9).</ P >
106
+ < P > Notice the use of Java annotations to declare the URI path, the
107
+ HTTP method and the media type. This is a key feature of JSR 311.</ P >
108
+ < H1 > Deploying the root resource</ H1 >
109
+ < P > The root resource will be deployed using the Grizzly Web
110
+ container.</ P >
111
+ < P > Create the following Java class in your project:</ P >
112
+ < PRE > 1 public class Main {
113
+ 2
114
+ 3 public static void main(String[] args) throws IOException {
115
+ 4
116
+ 5 final String baseUri = "http://localhost:9998/";
117
+ 6 final Map<String, String> initParams = new HashMap<String, String>();
118
+ 7
119
+ 8 initParams.put("com.sun.jersey.config.property.packages",
120
+ 9 "com.sun.jersey.samples.helloworld.resources");
121
+ 10
122
+ 11 System.out.println("Starting grizzly...");
123
+ 12 SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
124
+ 13 baseUri, initParams);
125
+ 14 System.out.println(String.format(
126
+ 15 "Jersey app started with WADL available at %sapplication.wadl\n” +
127
+ 16 “Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
128
+ 17 System.in.read();
129
+ 18 threadSelector.stopEndpoint();
130
+ 19 System.exit(0);
131
+ 20 }
132
+ 21 }</ PRE > < P >
133
+ The < CODE > Main</ CODE > class deploys the < CODE > HelloWorldResource</ CODE >
134
+ using the Grizzly Web container.</ P >
135
+ < P > Lines 8 to 9 creates an initialization parameter that informs the
136
+ Jersey runtime where to search for root resource classes to be
137
+ deployed. In this case it assumes the root resource class in the
138
+ package < FONT FACE ="Courier New, monospace "> com.sun.jersey.samples.helloworld.resources</ FONT >
139
+ (or in a sub-package of).</ P >
140
+ < P > Lines 12 to 13 deploys the root resource to the base URI
141
+ "< A HREF ="http://localhost:9998/ "> http://localhost:9998/</ A > "
142
+ and returns a Grizzly SelectorThread. The complete URI of the Hello
143
+ World root resource is "< A HREF ="http://localhost:9998/helloworld "> http://localhost:9998/helloworld</ A > ".</ P >
144
+ < P > Notice that no deployment descriptors were needed and the root
145
+ resource was setup in a few statements of Java code.</ P >
146
+ < H1 > Testing the root resource</ H1 >
147
+ < P > Goto the URI < A HREF ="http://localhost:9998/helloworld "> http://localhost:9998/helloworld</ A >
148
+ in your favourite browser.</ P >
149
+ < P > Or, from the command line use < CODE > curl:</ CODE > </ P >
150
+ < PRE STYLE ="margin-bottom: 0.2in "> < CODE > > curl http://localhost:9998/helloworld</ CODE > </ PRE > < H1 >
151
+ < A NAME ="created "> </ A > Here's one I created earlier</ H1 >
152
+ < 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 >
153
+ sample in the Java.Net maven repository.</ P >
154
+ < P > For developers wishing to get started by deploying a Web
155
+ application an equivalent sample, as a Web application, is shipped as
156
+ 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 >
157
+ sample.</ P >
158
+ </ BODY >
159
+ </ HTML >
0 commit comments