转载:
http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features
JAX-RS 1.0 Features
@Path
The @Path annotation's value is a relative URI path. In the example above, the Java class will be hosted at the URI path /helloworld. This is an extremely simple use of the @Path annotation. What makes JAX-RS so useful is that you can embed variables in the URIs.
URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. Variables are denoted by curly braces. For example, look at the following @Path annotation:
In this type of example, a user will be prompted to enter their name, and then a Jersey web service configured to respond to requests to this URI path template will respond. For example, if the user entered their username as "Galileo", the web service will respond to the following URL:
http://example.com/users/Galileo
To obtain the value of the username variable the @PathParam may be used on method parameter of a request method, for example:
If it is required that a user name must only consist of lower and upper case numeric characters then it is possible to declare a particular regular expression, which overrides the default regular expression, "[^/]+?", for example:
In this type of example the username variable will only match user names that begin with one upper or lower case letter and zero or more alpha numeric characters and the underscore character. If a user name does not match that a 404 (Not Found) response will occur.
A @Path value may or may not begin with a '/', it makes no difference. Likewise, by default, a @Path value may or may not end in a '/', it makes no difference, and thus request URLs that end or do not end in a '/' will both be matched. However, Jersey has a redirection mechanism, which if enabled, automatically performs redirection to a request URL ending in a '/' if a request URL does not end in a '/' and the matching @Path does end in a '/'.
HTTP Methods
@GET, @PUT, @POST , @DELETE, and @HEAD are request method designator annotations defined by JAX-RS and which correspond to the similarly named HTTP methods. In the example above, the annotated Java method will process HTTP GET requests. The behavior of a resource is determined by which of the HTTP methods the resource is responding to.
The following example is an extract from the storage service sample that shows the use of the PUT method to create or update a storage container:
By default the JAX-RS runtime will automatically support the methods HEAD and OPTIONS, if not explicitly implemented. For HEAD the runtime will invoke the implemented GET method (if present) and ignore the response entity (if set). For OPTIONS the the Allow response header will be set to the set of HTTP methods support by the resource. In addition Jersey will return a WADL document describing the resource.
@Produces
The @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client. In this example, the Java method will produce representations identified by the MIME media type "text/plain".
@Produces can be applied at both the class and method levels. Here's an example:
The doGetAsPlainText method defaults to the MIME type of the @Produces annotation at the class level. The doGetAsHtml method's @Produces annotation overrides the class-level @Produces setting, and specifies that the method can produce HTML rather than plain text.
If a resource class is capable of producing more that one MIME media type then the resource method chosen will correspond to the most acceptable media type as declared by the client. More specifically the Accept header of the HTTP request declared what is most acceptable. For example if the Accept header is:
then the doGetAsPlainText method will be invoked. Alternatively if the Accept header is:
which declares that the client can accept media types of "text/plain" and "text/html" but prefers the latter, then the doGetAsHtml method will be invoked.
More than one media type may be declared in the same @Produces declaration, for example:
The doGetAsXmlOrJson method will get invoked if either of the media types "application/xml" and "application/json" are acceptable. If both are equally acceptable then the former will be chosen because it occurs first.
The examples above refer explicitly to MIME media types for clarity. It is possible to refer to constant values, which may reduce typographical errors, see the constant field values of MediaType.
@Consumes
The @Consumes annotation is used to specify the MIME media types of representations a resource can consume that were sent by the client. The above example can be modified to set the cliched message as follows:
In this example, the Java method will consume representations identified by the MIME media type "text/plain". Notice that the resource method returns void. This means no representation is returned and response with a status code of 204 (No Content) will be returned.
@Consumes can be applied at both the class and method levels and more than one media type may be declared in the same @Consumes declaration.
分享到:
相关推荐
**JAX-RS方式的RESTful Web Service开发详解** RESTful Web Service是一种基于HTTP协议的、无状态的、客户端-服务器交互模式,它利用HTTP方法(GET、POST、PUT、DELETE等)来实现对资源的操作。JAX-RS是Java API ...
为了在Apache Tomcat服务器上部署Jersey创建的RESTful服务,开发者需要执行一系列步骤,如将Jersey库添加到项目的依赖中,配置web.xml来声明Jersey提供的Servlet,以及编写相应的资源类和方法。一旦这些步骤正确执行...
使用Java创建RESTful Web Service 在本文中,我们将讨论如何使用Java创建RESTful Web服务。REST(Representational State of Resource)是一种架构风格,它于2000年由Roy Fielding博士提出。RESTful Web服务是一种...
《Java Restful Web Service实战》源代码.zip是一个包含与Java Restful Web Service开发相关的实践项目源码的压缩包。在本文中,我们将深入探讨Java Restful Web Service的关键概念、技术栈以及如何通过实际示例来...
Java RESTful Web Service实战是Java开发领域中一个重要的实践教程,它主要涵盖了使用Java技术和RESTful架构风格来创建高效、可扩展的网络服务。REST(Representational State Transfer)是一种网络应用程序的设计...
在本文中,我们将深入探讨如何使用SpringBoot框架与Jersey库整合来实现RESTful Web服务,并同时集成Spring MVC。这将使我们能够构建一个高效、灵活的后端系统,为客户端提供API接口。 首先,SpringBoot是Spring框架...
### 使用Server-Sent Events (SSE) 实现RESTful Web Service #### 一、引言 在传统的Web服务中,客户端向服务器发送请求以获取所需资源。这种模式虽然简单直观,但在某些应用场景下存在局限性,尤其是在需要实时...
This book provides a comprehensive introduction to Jersey framework (an implementation of JAX-RS specification) as the application development framework for RESTFul web service development. This book ...
描述中的"RESTful web service for JBoss"意味着我们将讨论如何在JBoss应用服务器上开发和部署RESTful服务。JBoss提供了对JAX-RS规范的内置支持,使得开发者能够在其上快速构建REST接口。 标签"java"表明我们主要...
"Eclipse中使用Jersey和Tomcat构建RESTful WebService及其调用" RESTful Web服务简介 ----------------- ...使用Jersey和Tomcat构建RESTful Web服务可以提供灵活的Web服务开发体验,同时也可以提高开发效率和质量。
Java RESTful Web Service实战教程是Java开发者学习现代Web服务开发的重要资源。RESTful Web服务是一种基于Representational State Transfer(表述性状态转移)架构约束的Web服务设计风格,它强调轻量级、简单性和可...
### RESTful Web Services – 客户端API在Java中的应用:Jersey #### 一、RESTful Web Services 概念及特点 **REST**(Representational State Transfer)是一种用于设计网络应用程序的架构风格,其核心原则之一是...
Jersey 是 Sun Microsystems 公司推出的一款基于 Java 的 RESTful Web Service 开发框架,它遵循 JAX-RS(Java API for RESTful Web Services)规范。JAX-RS 规范(JSR 311)旨在简化 RESTful Web Service 的开发...
JSON Web Service库主要涉及到三个关键组件:ASM、Jersey和JSON。这些库文件在开发基于JSON的Web服务中起着至关重要的作用。 首先,我们来深入理解ASM-3.3.1.jar。ASM是一个Java字节码操控和分析框架,主要用于动态...
Jersey 是一个开源的 RESTful Web 服务框架,它基于 Java 的 JAX-RS(Java API for RESTful Web Services)规范。本示例源码旨在帮助开发者了解如何使用 Jersey 创建和实现 RESTful 服务。REST(Representational ...
通常,一个RESTful Web Service将定义基本资源URI、它所支持的表示/响应MIME,以及它所支持的操作。 本文将介绍如何使用Spring创建Java实现的服务器端RESTful Web Services。这个例子将使用浏览器、curl和Fire...
Jersey是Java平台上的一个开源项目,它是JAX-RS(Java API for RESTful Web Services)规范的参考实现。JAX-RS提供了一种标准的方式来创建RESTful服务。在Jersey中,你可以使用注解来定义资源类和方法,这些注解...
Jersey 是一个开源的 RESTful Web 服务客户端和服务器实现,它是 Java API for RESTful Web Services (JAX-RS) 规范的一个实现。JAX-RS 是 Java 平台上用于构建 RESTful 服务的标准接口,它使得开发人员能够轻松地...