Jersey 是一个JAX-RS的实现, JAX-RS即Java API for RESTful Web Services, 支持按照表述性状态转移(REST)架构风格创建Web服务. REST 中最重要的概念是资源(resources),使用Global ID (通常使用 URI)标识. 客户端应用程序使用 HTTP 方法 GET/ POST/ PUT/ DELETE 操作资源或资源集. RESTful Web 服务是使用 HTTP 和 REST 原理实现的 Web 服务. 通常, RESTful Web 服务应该定义以下方面:

Web 服务的基/根 URI,比如 http://host/<appcontext>/resources
支持 MIME 类型的响应数据,包括 JSON/XML/ATOM 等等
服务支持的操作集合, 例如 POST, GET, PUT 或 DELET.

JAX-RS 提供一个部署检测的抽象类Application用于声明根资源和提供对应的类, 在支持Servlet 3.0及以上的容器上, 可以不需要web.xml来部署. 仅需要在某个class上使用@ApplicationPath, 并扩展Application

  1. @ApplicationPath("/*")
  2. public class MyApplication extends Application {
  3. @Override
  4. public Set<Class<?>> getClasses() {
  5. Set<Class<?>> s = new HashSet<Class<?>>();
  6. s.add(HelloWorldResource.class);
  7. return s;
  8. }
  9. ...
  10. }

而Jersey提供了自己实现的方式, 可以更便利地使用其高级特性

  1. @ApplicationPath("resources")
  2. public class MyApplication extends ResourceConfig {
  3. public MyApplication() {
  4. packages("org.foo.rest;org.bar.rest");
  5. }
  6. }

此时要在pom.xml中添加 failOnMissingWebXml 以免构建中出现丢失web.xml的错误

  1. <plugins>
  2. ...
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-war-plugin</artifactId>
  6. <version>2.3</version>
  7. <configuration>
  8. <failOnMissingWebXml>false</failOnMissingWebXml>
  9. </configuration>
  10. </plugin>
  11. ...
  12. </plugins>

在MyApplication()的初始化方法中,

packages("com.aa.bb", "com.aa.cc");可以提供多个用于自动扫描的包路径. 这些路径下, 就是标注了 @Path, @Produces, @GET等信息的资源类.

register()方法用于注册各种组件

使用Maven创建一个Jersey项目:

  1. mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.22. -DinteractiveMode=false -DgroupId=com.nz -DartifactId=longinus -Dpackage=com.nz

如果要使用上面的servlet 3.0特性不带web.xml启动, 需要修改下pom.xml

注意里面的这段话

  1. <dependency>
  2. <groupId>org.glassfish.jersey.containers</groupId>
  3. <artifactId>jersey-container-servlet-core</artifactId>
  4. <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
  5. <!-- artifactId>jersey-container-servlet</artifactId -->
  6. </dependency>

需要去掉和servlet 2.x的兼容, 然后就能以不带web.xml的形式启动了

  1. <dependency>
  2. <groupId>org.glassfish.jersey.containers</groupId>
  3. <artifactId>jersey-container-servlet</artifactId>
  4. </dependency>

最后的pom.xml是这样的

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.  
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.rockbb</groupId>
  7. <artifactId>foobar</artifactId>
  8. <packaging>war</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <name>foobar</name>
  11.  
  12. <properties>
  13. <jersey.version>2.22.1</jersey.version>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <build>
  18. <finalName>foobar</finalName>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-compiler-plugin</artifactId>
  23. <version>3.3</version>
  24. <inherited>true</inherited>
  25. <configuration>
  26. <source>1.7</source>
  27. <target>1.7</target>
  28. <encoding>${project.build.sourceEncoding}</encoding>
  29. </configuration>
  30. </plugin>
  31. <plugin>
  32. <groupId>org.apache.maven.plugins</groupId>
  33. <artifactId>maven-resources-plugin</artifactId>
  34. <configuration>
  35. <encoding>${project.build.sourceEncoding}</encoding>
  36. </configuration>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-war-plugin</artifactId>
  41. <version>2.3</version>
  42. <configuration>
  43. <failOnMissingWebXml>false</failOnMissingWebXml>
  44. </configuration>
  45. </plugin>
  46. </plugins>
  47. </build>
  48.  
  49. <dependencyManagement>
  50. <dependencies>
  51. <dependency>
  52. <groupId>org.glassfish.jersey</groupId>
  53. <artifactId>jersey-bom</artifactId>
  54. <version>${jersey.version}</version>
  55. <type>pom</type>
  56. <scope>import</scope>
  57. </dependency>
  58. </dependencies>
  59. </dependencyManagement>
  60.  
  61. <dependencies>
  62. <dependency>
  63. <groupId>org.glassfish.jersey.containers</groupId>
  64. <artifactId>jersey-container-servlet</artifactId>
  65. </dependency>
  66. <!-- uncomment this to get JSON support
  67. <dependency>
  68. <groupId>org.glassfish.jersey.media</groupId>
  69. <artifactId>jersey-media-moxy</artifactId>
  70. </dependency>
  71. -->
  72. </dependencies>
  73. </project>

jersey常用注解解释:           

Annotation 作用 说明
@GET 查询请求 相当于数据库的查询数据操作
@POST 插入请求 相当于数据库的插入数据操作
@PUT 更新请求 相当于数据库的更新数据操作
@DELETE 删除请求 相当于数据的删除数据操作
@Path uri路径 定义资源的访问路径,client通过这个路径访问资源。比如:@Path("user")
@Produces 指定返回MIME格式 资源按照那种数据格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML)
@Consumes 接受指定的MIME格式 只有符合这个参数设置的请求再能访问到这个资源。比如@Consumes("application/x-www-form-urlencoded")
@PathParam uri路径参数 写在方法的参数中,获得请求路径参数。比如:@PathParam("username")  String userName
@QueryParam uri路径请求参数 写在方法的参数中,获得请求路径附带的参数。比如:@QueryParam("desc")  String desc
@DefaultValue 设置@QueryParam参数的默认值 如果@QueryParam没有接收到值,就使用默认值。比如:@DefaultValue("description") @QueryParam("desc") String desc
@FormParam form传递的参数 接受form传递过来的参数。比如:@FormParam("name")  String userName
@BeanParam 通过Bena的形式传递参数 接受client传递的bean类型的参数,同时这个bean可以在属性上配置@FormParam用以解决client的属性名称和bean的属性名称不一致的问题。比如:@BeanParam  User user
@Context 获得一些系统环境信息 通过@Context可以获得以下信息:UriInfo、ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse和HttpHeaders等
@XmlRootElement 将bean转换为xml

如果要讲bean以xml或json的格式返回,必须要这个注解。比如:

@XmlRootElement

public class User{...}

@XmlElements
@XmlElement

Jersey the RESTful Web Services in Java的更多相关文章

  1. 使用 Spring 3 来创建 RESTful Web Services(转)

    使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...

  2. Spring 3 来创建 RESTful Web Services

    Spring 3 创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 ...

  3. jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档

    原文:https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS) Content Tuto ...

  4. 使用 Spring 3 来创建 RESTful Web Services

    来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...

  5. 就是这么简单!使用Rest-assured 测试Restful Web Services

    使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...

  6. cxf开发Restful Web Services

    一.restful web services rest全称是Representation State Transfer(表述性状态转移).它是一种软件架构风格,只是提供了一组设计原则和约束条件.在re ...

  7. RESTful Web Services测试工具推荐

    命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...

  8. 基于Spring设计并实现RESTful Web Services(转)

    基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...

  9. RESTful Web Services: A Tutorial--reference

    As REST has become the default for most Web and mobile apps, it's imperative to have the basics at y ...

随机推荐

  1. Android下创建一个SQLite数据库

    数据库:SQLite(轻量级,嵌入式的数据库) 大量的相似结构的数据的储存,快速的查询.特殊的文件(按照一定的格式生成) 数据库的创建 创建文件 1.声明文件对象,文件是不会被创建出来的. File ...

  2. 关于android的一些基础知识

    怕自己以后忘了,所以在这里先写写! equal和==的区别是,一个用于判断字符串,一个用于判断int是否相等 equal比较的是对象,==比较的是值

  3. Facebook开源动画库 POP-POPDecayAnimation运用

    关于POPDecayAnimation的介绍先引用别人写的一些内容,基本上把它的一些注意点都说明了: Decay Animation 就是 POP 提供的另外一个非常特别的动画,他实现了一个衰减的效果 ...

  4. class&meta class

    http://chun.tips 当我们发送一个消息给一个NSObject对象时,这条消息会在对象的类的方法列表里查找当我们发送一个消息给一个类时,这条消息会在类的Meta Class的方法列表里查找 ...

  5. 哭瞎!360云盘将关停,你的几十T照片和文件该怎么办

    IDO老徐刚得到了一个非常不开心的消息,360云盘将停止个人云盘服务...进行业务转型,在网盘存储.传播内容的合法性和安全性得到彻底解决之前不再考虑恢复,之后转型企业云服务. 而且之前共享的所有资料, ...

  6. BIEE建模参考规范

    BIEE建模参考规范 注:本文基于网上盛传的“BIEE建模黄金法则”,并做了更为细致的讲解,以及修改. 物理层 1.  在可能的情况下,配置你的连接池使用本地驱动来连接物理数据库.例如,使用OCI而不 ...

  7. Mongodb Manual阅读笔记:MongoDB教程

    Mongodb教程的说明,可以当手册用 Getting Started Install MongoDB on Linux Systems Install MongoDB on Red Hat Ente ...

  8. Mongodb Manual阅读笔记:CH3 数据模型(Data Models)

    3数据模型(Data Models) Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mon ...

  9. mysql优化---订单查询优化:视图优化+索引创建

    订单的表结构采用了垂直分表的策略,将订单相关的不同模块的字段维护在不同表中 在订单处理这个页面,需要查询各种维度, 因此为了方便查询创建了v_sale_order视图(老版本) drop view v ...

  10. linux的“自动化”

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...