Jersey the RESTful Web Services in Java
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
@ApplicationPath("/*")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
return s;
}
...
}
而Jersey提供了自己实现的方式, 可以更便利地使用其高级特性
@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("org.foo.rest;org.bar.rest");
}
}
此时要在pom.xml中添加 failOnMissingWebXml 以免构建中出现丢失web.xml的错误
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
...
</plugins>
在MyApplication()的初始化方法中,
packages("com.aa.bb", "com.aa.cc");可以提供多个用于自动扫描的包路径. 这些路径下, 就是标注了 @Path, @Produces, @GET等信息的资源类.
register()方法用于注册各种组件
使用Maven创建一个Jersey项目:
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
注意里面的这段话
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
需要去掉和servlet 2.x的兼容, 然后就能以不带web.xml的形式启动了
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
最后的pom.xml是这样的
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rockbb</groupId>
<artifactId>foobar</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>foobar</name> <properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <build>
<finalName>foobar</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
</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的更多相关文章
- 使用 Spring 3 来创建 RESTful Web Services(转)
使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...
- Spring 3 来创建 RESTful Web Services
Spring 3 创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参考实现 ...
- 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 ...
- 使用 Spring 3 来创建 RESTful Web Services
来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...
- 就是这么简单!使用Rest-assured 测试Restful Web Services
使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...
- cxf开发Restful Web Services
一.restful web services rest全称是Representation State Transfer(表述性状态转移).它是一种软件架构风格,只是提供了一组设计原则和约束条件.在re ...
- RESTful Web Services测试工具推荐
命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...
- 基于Spring设计并实现RESTful Web Services(转)
基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...
- 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 ...
随机推荐
- Xcode快捷键大全
转载地址http://www.360doc.com/content/12/0521/09/6541311_212458595.shtml.
- OC中的内存管理
一. 基本原理 1. 什么是内存管理 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间.比如回收一些不需 ...
- 学习Coding-iOS开源项目日志(一)
前言:作为初级程序员,想要提高自己的水平,其中一个有效的学习方法就是学习别人好的项目.本篇开始会陆续更新本人对github上开源的一个很不错的项目的一点点学习积累.也就是,探究着别人写的源码,我学到了 ...
- 关于Linux与windows传递文件乱码问题
linux下一般是采用utf-8的编码,而我们在windows上编辑文件时是gb2312的编码.所以导致中文编码会乱码.要更正这个问题实际上很简单只要把文件转换成utf-8编码格式然后再导入就ok啦. ...
- CSS Float 以及相关布局模式
float 取值 属性 值 描述 left 向左浮动 right 向右浮动 none 默认值 inherit 继承 看一个栗子 红色线框代表父元素 脱离文档流,其实也没有完全脱离,会被 ...
- post请求报文
POST /02_WEB_HTTP/index.html HTTP/1.1 Accept: application/x-ms-application, image/jpeg, application/ ...
- CMPP错误码说明
与中国移动代码的对应关系. MI::zzzzSMSC返回状态报告的状态值为EXPIREDMJ:zzzzSMSC返回状态报告的状态值为DELETEDMK:zzzzSMSC返回状态报告的状态值为UNDEL ...
- Mongodb Manual阅读笔记:CH6 聚合
6 聚合 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔 ...
- Maven详细介绍
Maven 目录 1 什么是Maven? 2 Maven 的好处 3 获取和安装 3.1 获取 3.2 安装 3.2.1 环境变量的配置 4 设置本地仓库 5 创建简单的Maven实例 5.1 使用骨 ...
- itextpd f生成 pdf 文件
一.简介 itextpdf 是一个开源的允许你去创建和操作PDF文档的库.它使的开发者可以提高web和其他应用来动态地生成或操作PDF文档.通过iText 中的Document和PdfWriter类, ...