Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency
<properties>
<jersey.version>1.19.1</jersey.version>
</properties> <dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
</dependency>
</dependencies>
2. JavaBean
package com.huey.hello.jersey.bean; import javax.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @NoArgsConstructor
@AllArgsConstructor
@Data
@XmlRootElement
public class Book { private String title;
private String[] author;
private String publisher;
private double price;
private String isbn; }
3. RESTful Web Services
package com.huey.hello.jersey.service; import java.util.ArrayList;
import java.util.List; import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType; import com.huey.hello.jersey.bean.Book; @Path("/books")
public class BookService { private static List<Book> books; static {
books = new ArrayList<Book>();
books.add(new Book("高性能MySQL",
new String[]{"Baron Schwartz", "Peter Zaitsev"},
"电子工业出版社",
128.00,
"9787121198854"));
books.add(new Book("HTTP权威指南",
new String[]{"David Gourley", "Brian Totty"},
"人民邮电出版社",
109.00,
"9787115281487"));
} @GET
@Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
public List<Book> getBooks() {
return books;
} @GET
@Path("/{isbn}")
@Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
public Book getBook(@PathParam("isbn") String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
return book;
}
}
throw new WebApplicationException(404);
} @POST
@Consumes( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
public void addBook(Book book) {
books.add(book);
} }
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>hello-jersey</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>\
</welcome-file-list> <servlet>
<servlet-name>JerseyRESTService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.huey.hello.jersey.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRESTService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
5. 部署启动 Web 工程。
6. 测试。
a) getBook
[huey@huey-K42JE ~]$ curl -i http://localhost:8080/hello-jersey/rest/books/9787115281487
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 11 Apr 2016 05:03:04 GMT {"author":["David Gourley","Brian Totty"],"isbn":"9787115281487","price":"109.0","publisher":"人民邮电出版社","title":"HTTP权威指南"}
b) addBook
[huey@huey-K42JE ~]$ curl -i -H "Content-Type: application/json" -d '{"author":["Bruce Snyder","Dejan Bosanac"],"isbn":"9781933988948","price":"109.0","publisher":"Manning Publications","title":"ActiveMQ in Action"}' http://localhost:8080/hello-jersey/rest/books/
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Date: Mon, 11 Apr 2016 04:59:55 GMT
c) getBooks
[huey@huey-K42JE ~]$ curl -i -H "Accept: application/xml" http://localhost:8080/hello-jersey/rest/books/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/xml
Content-Length: 649
Date: Mon, 11 Apr 2016 05:07:15 GMT <?xml version="1.0" encoding="UTF-8" standalone="yes"?><books><book><author>Baron Schwartz</author><author>Peter Zaitsev</author><isbn>9787121198854</isbn><price>128.0</price><publisher>电子业出版社</publisher><title>高性能MySQL</title></book><book><author>David Gourley</author><author>Brian Totty</author><isbn>9787115281487</isbn><price>109.0</price><publisher>人民邮电出版社</publisher><title>HTTP权威指南</title></book><book><author>Bruce Snyder</author><author>Dejan Bosanac</author><isbn>9781933988948</isbn><price>109.0</price><publisher>Manning Publications</publisher><title>ActiveMQ in Action</title></book></books>
Jersey(1.19.1) - Hello World, Get started with a Web application的更多相关文章
- 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) - 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> ...
- 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) - 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 ...
随机推荐
- memcached全面剖析–4. memcached的分布式算法
memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现 ...
- AppDelegate 、UIApplication 的用法
转载自 http://blog.chinaunix.net/uid-26768267-id-3300042.html //AppDelegate.h 头文件 #import <UIKit/UI ...
- WebForm 回传后如何保持页面的滚动位置
转载自 http://www.cnblogs.com/renjuwht/archive/2009/06/17/1505000.html 默认情况下,ASP.NET页面回传到服务器后,页面会跳回顶部.对 ...
- C++中用二维数组传参时形参该怎样写[转]
二维数组的存储方式是和一维数组没什么区别,但是用二维数组做参数,它的形参该怎样写? 要注意的是:函数中的形参其实就相当于一个声明,并不产生内存分配,形参的目的就是要让编译器知道函数参数的数据类型. 正 ...
- mac 杂谈
========== 下载 -o 文件名:-O默认文件名保存 curl -o baidu.hml http://www.baidu.com curl -O http://su.bdimg.com/st ...
- Smarty模板中调用PHP函数
因为应用需要,要在Smarty中调用PHP函数,实现办法如下:模板 数据条数:{$data|count} 活动页面文件后缀:{$page|substr:'-3'} 特殊情况:{$page|str_re ...
- VS DLL 复制本地
1.引用一个DLL,需要指定路径,复制本地的意思是 把这个DLL复制到exe的Debug目录(调试的时候). 2.复制到本地的动作是在生成的时候执行的,清理的时候会删除. 3.从外部引用一个DLL,不 ...
- C#中判断空字符串的3种方法性能分析
3种方法分别是:string a="";1.if(a=="")2.if(a==String.Empty)3.if(a.Length==0) 3种方法都是等效的, ...
- MHA手动切换 原创1(主故障)
MHA提供了3种方式用于实现故障转移,分别自动故障转移,需要启用MHA监控: 在无监控的情况下的手动故障转移以及基于在线手动切换. 三种方式可以应对MySQL主从故障的任意场景.本文主要描述在无监控的 ...
- SSO-Javascript模拟IE登录,不让IIS弹出登录窗口
解决方案: 用JS模拟IE用户登录,再跳转到对应的系统. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...