Jersey(1.19.1) - Representations and Java Types
Previous sections on @Produces and @Consumes referred to MIME media types of representations and showed resource methods that consume and produce the Java type String for a number of different media types. However, String is just one of many Java types that are required to be supported by JAX-RS implementations.
Java types such as byte[], java.io.InputStream, java.io.Reader and java.io.File are supported. In addition JAXB beans are supported. Such beans are JAXBElement or classes annotated with @XmlRootElement or @XmlType. The samples jaxb and json-from-jaxb show the use of JAXB beans.
Unlike method parameters that are associated with the extraction of request parameters, the method parameter associated with the representation being consumed does not require annotating. A maximum of one such unannotated method parameter may exist since there may only be a maximum of one such representation sent in a request.
The representation being produced corresponds to what is returned by the resource method. For example JAX-RS makes it simple to produce images that are instance of File
as follows:
@GET
@Path("/images/{image}")
@Produces("image/*")
public Response getImage(@PathParam("image") String image) {
if (!isSafeToOpenFile(image)) {
throw new IllegalArgumentException("Cannot open the image file.");
} File file = new File(image);
if (!file.exists()) {
throw new WebApplicationException(404);
} String mimeType = new MimetypesFileTypeMap().getContentType(file);
return Response.ok(file, mimeType).build();
}
A File
type can also be used when consuming, a temporary file will be created where the request entity is stored.
The Content-Type (if not set, see next section) can be automatically set from the MIME media types declared by @Produces if the most acceptable media type is not a wild card (one that contains a *, for example "application/" or "/*"). Given the following method:
@GET
@Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
...
}
if "application/xml" is the most acceptable then the Content-Type
of the response will be set to "application/xml".
Jersey(1.19.1) - Representations and Java Types的更多相关文章
- 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) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- 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 ...
- Jersey(1.19.1) - XML Support
As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incomin ...
- Jersey(1.19.1) - Extracting Request Parameters
Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...
- Jersey(1.19.1) - Use of @Context
Previous sections have introduced the use of @Context. The JAX-RS specification presents all the sta ...
- 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) - 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> ...
随机推荐
- AngularJS 学习笔记一
ng-app 指令 ng-app 指令定义了 AngularJS 应用程序的 根元素. ng-app 指令在网页加载完毕时会自动引导(自动初始化)应用程序. 稍后您将学习到 ng-app 如何通过一个 ...
- 什么是APNs证书?
转载自 http://dev.xiaomi.com/doc/p=2977/index.html 什么是APNs和APNs证书? APNs(全称为Apple Push Notification Serv ...
- JQuery中的AJAX参数详细介绍
Jquery中AJAX参数详细介绍 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求方 ...
- 正整数的n次方求和
引理: (Abel分部求和法) $$\sum_{k=1}^{n}a_{k}b_{k}=A_{n}b_{n}+\sum_{k=1}^{n-1}A_{k}(b_{k}-b_{k+1})$$其中$A_{k} ...
- getDefinitionByName与ApplicationDomain.getDefinition
主swf 定义:MC,被加载swf 定义:MC.MC1 ①父SWF的应用程序域的新建子域 (默认方式)称其为应用程序域的继承 var app:ApplicationDomain=new Applica ...
- Java 中队列的使用
刚才看见群里的一个朋友在问队列的使用,确实在现实的写代码中非常少使用队列的,反正我是从来没使用过.仅仅是学数据结构的时候学过. 以下是我写的一个小样例,希望有不足之处请提出改正.O(∩_∩)O~ 看代 ...
- IOS 小技巧积累
转自:http://blog.csdn.net/mars2639/article/details/7352012 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在 ...
- python读取文本、配对、插入数据脚本
#在工作中遇见了一个处理数据的问题,纠结了很久,写下记录一下.#-*- coding:UTF-8 -*- #-*- author:ytxu -*- import codecs, os, sys, pl ...
- Asp.Net 之 网页快照
此文做法不是 Control.DrawToBitmap ,而是直接QueryInterface 浏览器Com对象的 IViewObject 接口,用它实现的Draw方法,画到图像上. 首先,定义IVi ...
- SSO 登录功能的实现
一.引言 自己早晚都会碰到的问题. 当需要到分离多站点多应用的时候,都是希望用户只要在一个站点登录,其它所有的应用站点都是已登录的状态. 查了下新浪与淘宝的登录的资料,自己实现了一个并做下记录. 二. ...