Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server
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的更多相关文章
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- Jersey(1.19.1) - Root Resource Classes
Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...
- Jersey(1.19.1) - Deploying a RESTful Web Service
JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and pro ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- C#学习笔记(六):可空类型、匿名方法和迭代器
可空类型 为啥要引入可空类型? 在数据库中,字段是可以为null值的,那么在C#中为了方便的操作数据库的值,微软引入了可空类型. 声明可空类型 我们可以使用两种方法声明一个可空类型: Nullable ...
- MemCacheManager
#region Fields private AreaRepository _areaRepository = new AreaRepository(); private ICacheManager ...
- NHibernate - ICriteria 查询
http://blog.knowsky.com/213234.htm http://blog.chinaunix.net/uid-20463341-id-1673509.html http://www ...
- const修饰的双重指针赋值解惑
在c程序中,我们可能经常会使用到指针之间的赋值. 传统的赋值操作: char *cp = "c"; const char *ccp; ccp = cp; printf(" ...
- 虚拟机VMware里 windows server 2003 扩充C盘方法
你会经常用windows server 2003 吗?应该不会吧,有时一些东西必须装在windows server 2003 上才能用,所以 用虚拟机把,好,装在虚拟机上,8G的C盘够你用吗,一个稍微 ...
- Unity3D-Baked Lightmapping 示例学习
首先,看一下摄像机的Rendering Paths http://game.ceeger.com/Manual/RenderingPaths.html 可以看出,对于灯光的渲染质量 Deferred ...
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学
B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- WWDC2015—图解
- apache配置--虚拟目录
apache在httpd-vhosts.conf中 配置二级域名或者泛域名: <VirtualHost *:80> ServerAdmin 846606478@qq.com D ...