Jersey(1.19.1) - Hello World, Get started with a Web application
2016-04-10 23:01
441 查看
1. Maven Dependency
View Code
2. JavaBean
3. RESTful Web Services
4. web.xml
5. 部署启动 Web 工程。
6. 测试。
a) getBook
b) addBook
c) getBooks
<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>
View Code
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>
相关文章推荐
- Android中的Audio播放:竞争Audio之Audio Focus的应用
- [Android]Android Studio gradle配置-20160410
- android返回主页时,Activity在后台继续运行的方法
- android工具集合与api参考
- This application has requested the Runtime to terminate it in an unusual way
- iOS自学(4)
- Android 5.0状态栏和导航栏
- iOS学习笔记15-序列化、偏好设置和归档
- Android res/raw 和 assets 使用
- iOS录音简单实现
- webview 使用
- Android的Touch事件处理和手势
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- CSAPP 学习笔记 Chapter one
- Swift Core Data 图片存储与读取Demo
- 【日常吐槽 · 第二期】向cocos的更新速度致敬
- iOS开发笔记:实现点击图片放大全屏
- Android自定义View之Layout的放缩(1)
- Android启动模式总结
- 捕获Application全局异常