jersey started

Getting Started

Getting started with Jersey is very easy. First, it is necessary depend on the correct Jersey artifacts as described in the dependences document.

Maven developers require a dependency on the jersey-server module, the grizzly-servlet-webserver module and optionally for WADL support if using Java SE 5 the jaxb-impl module.

Non-maven developers require:

grizzly-servlet-webserver.jar , jersey-server.jar , jersey-core.jar , jsr311-api.jar , asm.jar

and optionally for WADL support if using Java SE 5:

jaxb-impl.jar , jaxb-api.jar , activation.jar , stax-api.jar

Then, 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 section of this document.)

Creating a root resource

Create the following Java class in your project:

 1    // The Java class will be hosted at the URI path "/helloworld"
2 @Path("/helloworld")
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 "text/plain"
9 @Produces("text/plain")
10 public String getClichedMessage() {
11 // Return some cliched textual content
12 return "Hello World";
13 }
14 }

The HelloWorldResource class is a very simple Web resource. The URI path of the resource is "/helloworld" (line 2), it supports the HTTP GET method (line 6) and produces cliched textual content (line 12) of the MIME media type "text/plain" (line 9).

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.

Deploying the root resource

The root resource will be deployed using the Grizzly Web container.

Create the following Java class in your project:

 1    public class Main {
2
3 public static void main(String[] args) throws IOException {
4
5 final String baseUri = "http://localhost:9998/";
6 final Map<String, String> initParams = new HashMap<String, String>();
7
8 initParams.put("com.sun.jersey.config.property.packages",
9 "com.sun.jersey.samples.helloworld.resources");

10
11 System.out.println("Starting grizzly...");
12 SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
13 baseUri, initParams);
14 System.out.println(String.format(
15 "Jersey app started with WADL available at %sapplication.wadl/n” +
16 “Try out %shelloworld/nHit enter to stop it...", baseUri, baseUri));
17 System.in.read();
18 threadSelector.stopEndpoint();
19 System.exit(0);
20 }
21 }

The Main class deploys the HelloWorldResource using the Grizzly Web container.

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 com.sun.jersey.samples.helloworld.resources (or in a sub-package of).

Lines 12 to 13 deploys the root resource to the base URI “"http://localhost:9998/ ” and returns a Grizzly SelectorThread. The complete URI of the Hello World root resource is "http://localhost:9998/helloworld".

Notice that no deployment descriptors were needed and the root resource was setup in a few statements of Java code.

Testing the root resource

Goto the URI http://localhost:9998/helloworld in your favourite browser.

Or, from the command line use curl:

    > curl http://localhost:9998/helloworld

Here's one I created earlier

The example code presented above is shipped as the HelloWorld sample in the Java.Net maven repo.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值