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. HDU 4432 Sum of divisors (水题,进制转换)

    题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...

  2. MFC编辑框换行实现

    MFC中换行实现 在mfc中编辑框允许输入多行时,换行符被表示为<归位><换行>即“\r\n”,用ascii码表示为13 10 如果为编辑框中想要输入换行,就请将编辑框的属性: ...

  3. Castle IOC容器快速入门

    主要内容 1.为什么要IOC 2.什么是Castle IOC容器 3.快速入门示例 4.几个重要的概念 一,为什么要IOC IOC(控制反转或者叫依赖注入)Martin Fowler大师在他的文章中已 ...

  4. HttpModule的认识与深入理解及MVC运行机制

    转自:http://kb.cnblogs.com/page/50130/ ASP.NET MVC架构与实战系列之二:理解MVC路由配置 http://www.cnblogs.com/jyan/arch ...

  5. Ecshop实现注册页面手机号唯一的验证

    前天,公司总监提了一个需求,实现我公司商城注册会员用手机号登录这个功能,那么这个功能容易修改,在我的前一篇博文中已经给出处理方法了,但是这里有一个问题,就是如果实现了用手机号码来登陆,那么就需要在注册 ...

  6. C#关于静态与非静态的区别

    C#静态方法与非静态方法的区别不仅仅是概念上的,那么他们有什么具体的区别呢?让我们通过本文向你做一下解析. C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用 ...

  7. Debug with jdb

    原文地址: http://www.javaworld.com/article/2077445/testing-debugging/debug-with-jdb.html Q: How do you u ...

  8. 有关java中static关键的重写问题

    <Java编程思想>中这样提到“只有普通的方法调用可以是多态的”.说白了,就是静态方法不能实现重写这种多态. JAVA静态方法形式上可以重写(只要子类不加@Override关键字修饰的话, ...

  9. Visual Studio 调用 Delphi DLL 会退出的解决方案

    新写了一个 Delphi 的 dll 供 C# 程序调用,却发现在使用 Visual Studio 2012 进行调试时,程序会在调用后自动退出. 经过对比,只需要将工程属性中“调试”一页下的“启用 ...

  10. Caching in ASP.NET MVC

    The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, ...