Maven Dependencies

The following Maven dependencies need to be 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:

package com.huey.hello.jersey.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces; // The Java class will be hosted at the URI path "/helloworld"
@Path("helloworld")
public class HelloWorldResource { // The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media type "text/plain"
@Produces("text/plain")
public String 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 will be deployed using the Grizzly Web container.

 package com.huey.hello.jersey;

 import java.io.IOException;
import java.net.URI; import javax.ws.rs.core.UriBuilder; import org.glassfish.grizzly.http.server.HttpServer; import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig; public class HelloJersey { private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(9998).build();
} public static final URI BASE_URI = getBaseURI(); protected static HttpServer startServer() throws IOException {
System.out.println("Starting grizzly...");
ResourceConfig rc = new PackagesResourceConfig("com.huey.hello.jersey.resources");
return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
} public static void main(String[] args) throws IOException {
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
BASE_URI, BASE_URI));
System.in.read();
httpServer.stop();
}
}

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

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

Line 25 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:

[huey@huey-K42JE ~]$ curl -i http://localhost:9998/helloworld
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Wed, 13 Apr 2016 14:14:40 GMT
Transfer-Encoding: chunked Hello World

Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server的更多相关文章

  1. Jersey(1.19.1) - Hello World, Get started with a Web application

    1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...

  2. Jersey(1.19.1) - JSON Support

    Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...

  3. Jersey(1.19.1) - Root Resource Classes

    Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...

  4. Jersey(1.19.1) - Deploying a RESTful Web Service

    JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and pro ...

  5. Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses

    Previous sections have shown how to return HTTP responses and it is possible to return HTTP errors u ...

  6. Jersey(1.19.1) - Life-cycle of Root Resource Classes

    By default the life-cycle of root resource classes is per-request, namely that a new instance of a r ...

  7. Jersey(1.19.1) - Client API, Uniform Interface Constraint

    The Jersey client API is a high-level Java based API for interoperating with RESTful Web services. I ...

  8. Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts

    Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...

  9. Jersey(1.19.1) - Client API, Overview of the API

    To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...

随机推荐

  1. javascript实现颜色渐变

    渐变(Gradient)是美学中一条重要的形式美法则,与其相对应的是突变.形状.大小.位置.方向.色彩等视觉因素都可以进行渐变.在色彩中,色相.明度.纯度也都可以产生渐变效果,并会表现出具有丰富层次的 ...

  2. ASP.NET网站中设置404自定义错误页面

    在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...

  3. upload.php --->文件上传

    <?php header("Content-type:text/html;charset=utf-8"); print_r($_FILES['file']); $filena ...

  4. opencv 矩阵的相似性对比 (图片之间比较)

    测试图片:     code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\c ...

  5. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  6. skyline TerraExplorer fly设置相对路径的方法

    软件环境:TerraExplorer Pro(以下简称TEP)6.5 在TEP中,对于本地(非网络)文件路径,默认都是绝对路径,在移动数据时非常麻烦,以下是本人总结出一些设置相对路径的规则 假设fly ...

  7. Oracle DB 通过 Oracle Enterprise Manager注册要使用的恢复目录

    通过 Oracle Enterprise Manager  注册要使用的恢复目录.  a)  在 EM 中,导航到“Availability > Recovery Catalog Setting ...

  8. [Javascript] Either Functor

    Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...

  9. 给出一个数组A,找出一对 (i, j)使得A[i] <= A[j] (i < j)并且j-i最大

    题目:给出一个数组A,找出一对 (i, j)使得A[i] <= A[j] (i <= j)并且j-i最大 ,若有多个这样的位置对,返回i最小的那一对. 最直接的想法就是对于每一个 i 从数 ...

  10. 如何通过VIM把代码格式化后生成HTML网页代码

    本文转自http://wangxiaoyu.blog.51cto.com/922065/203471 需求及思路:演示需要,需要网站上嵌入一些代码,我的建议做法是根据代码文件,生成相应的HTML代码, ...