Maven Dependencies
The following Maven dependencies need to being added to the POM:
<Dependency> <groupId>Com.sun.jersey</groupId> <Artifactid>Jersey-server</Artifactid> <version>1.19.1</version></Dependency><Dependency> <groupId>Com.sun.jersey</groupId> <Artifactid>Jersey-grizzly2</Artifactid> <version>1.19.1</version></Dependency>
Creating a root resource
Create the following Java class in your project:
Packagecom.huey.hello.jersey.resources;ImportJavax.ws.rs.GET;ImportJavax.ws.rs.Path;Importjavax.ws.rs.Produces;//the Java class would be hosted at the URI path "/helloworld"@Path ("HelloWorld") Public classHelloworldresource {//The Java method would process HTTP GET requests@GET//the Java method would produce content identified by the MIME Media type "Text/plain"@Produces ("Text/plain") PublicString SayHello () {//Return the textual content return"Hello World"; } }
The Helloworldresource class is a very simple Web resource. The URI path of the resource is "/helloworld", it supports the HTTP GET method and produces cliched textual content of the MIME Media Type "Text/plain".
Deploying the root Resource
The root resource'll be deployed using the Grizzly Web container.
1 PackageCom.huey.hello.jersey;2 3 Importjava.io.IOException;4 ImportJava.net.URI;5 6 ImportJavax.ws.rs.core.UriBuilder;7 8 ImportOrg.glassfish.grizzly.http.server.HttpServer;9 Ten Importcom.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; One ImportCom.sun.jersey.api.core.PackagesResourceConfig; A ImportCom.sun.jersey.api.core.ResourceConfig; - - Public classHellojersey { the - Private StaticURI Getbaseuri () { - returnUribuilder.fromuri ("http://localhost/"). Port (9998). Build (); - } + - Public Static FinalURI Base_uri =Getbaseuri (); + A protected StaticHttpserver StartServer ()throwsIOException { atSystem.out.println ("Starting grizzly ..."); -ResourceConfig rc =NewPackagesresourceconfig ("com.huey.hello.jersey.resources"); - returngrizzlyserverfactory.createhttpserver (Base_uri, RC); - } - - Public Static voidMain (string[] args)throwsIOException { inHttpserver Httpserver =startserver (); -System.out.println (String.Format ("Jersey app started with WADL available at" to+ "%sapplication.wadl\ntry out%shelloworld\nhit Enter to stop it ...", + Base_uri, Base_uri)); - System.in.read (); the httpserver.stop (); * } $}
the HelloJersey
class deploys HelloWorldResource
the using the Grizzly Web container.
Line creates a initialization parameter that informs the Jersey runtime where to search for root resource classes to B E deployed. In this case it assumes the root resource class in the package com.huey.hello.jersey.resources (or in a Sub-packa GE of).
Line 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 '.
Testing the root Resource
Goto the URI Http://localhost:9998/helloworld in your favourite browser.
Or, from the command line use curl
:
Jersey (1.19.1)-Hello World, Get started with Jersey using the embedded Grizzly server