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> ...
随机推荐
- POJ2001Shortest Prefixes(字典树)
题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...
- POJ3630Phone List(字典树)
参考http://s.acmore.net/show_article/show/58 以附上代码 #include<iostream> #include<stdio.h> #i ...
- HDU/杭电2013多校第三场解题报告
今天悲剧了,各种被虐啊,还是太年轻了 Crime 这道题目给的时间好长,第一次就想到了暴力,结果华丽丽的TLE了. 后来找了一下,发现前24个是1, 2, 6, 12, 72, 72, 864, 17 ...
- libevent的使用方法--回显服务器的简单实例
#include <event.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...
- Installation Directory must be on a local hard drive解决办法
今天带着公司的电脑来杭州这边,同事发来一个Sliksubversion.msi来进行安装,由于系统是win8.1的,直接点击安装不了,真的是醉了,于是乎发挥度娘的力量找到了答案,这里贴出来,供大家来参 ...
- linux 下httpd服务开机启动
分类: 网站搭建2011-01-07 05:52 1164人阅读 评论(0) 收藏 举报 linuxapache 我的apache安装目录在 /usr/local/apache 有2种方法可以设置开机 ...
- 【多校练习4签到题】HDU 4642—— Fliping game
来源:点击打开链接 看上去很难,比赛的时候光看hehe了,也没有想. 但是仔细想想,是可以想出来的.一个棋盘上每个格子摆放一个硬币,硬币有正面1和反面0之分.现在两个人可以按照规则翻硬币,选择(x,y ...
- JQuery UI Widget Factory官方Demo
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- BZOJ 2208: [Jsoi2010]连通数 tarjan bitset
2208: [Jsoi2010]连通数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- sharepoint 2013 未能加载类型"Microsoft.AnalysisServices.SharePoint.Integration.ReportGalleryView"
最近在做PowerPivot for sharepoint server 2013的时候,创建PowerPivot库,之后打开,出现了一个问题: 未能加载类型"Microsoft.Analy ...