Conditional GETs are a great way to reduce bandwidth, and potentially server-side performance, depending on how the information used to determine conditions is calculated. A well-designed web site may return 304 (Not Modified) responses for the many of the static images it serves.

JAX-RS provides support for conditional GETs using the contextual interface Request.

The following example shows conditional GET support from the sparklines sample:

public SparklinesResource(
@QueryParam("d") IntegerList data,
@DefaultValue("0,100") @QueryParam("limits") Interval limits,
@Context Request request,
@Context UriInfo ui) { if (data == null)
throw new WebApplicationException(400); this.data = data; this.limits = limits; if (!limits.contains(data))
throw new WebApplicationException(400); this.tag = computeEntityTag(ui.getRequestUri());
if (request.getMethod().equals("GET")) {
Response.ResponseBuilder rb = request.evaluatePreconditions(tag);
if (rb != null)
throw new WebApplicationException(rb.build());
}
}

The constructor of the SparklinesResouce root resource class computes an entity tag from the request URI and then calls the request.evaluatePreconditions with that entity tag. If a client request contains an If-None-Match header with a value that contains the same entity tag that was calculated then the evaluatePreconditionsreturns a pre-filled out response, with the 304 status code and entity tag set, that may be built and returned. Otherwise, evaluatePreconditions returns null and the normal response can be returned.

Notice that in this example the constructor of a resource class can be used perform actions that may otherwise have to be duplicated to invoked for each resource method.

Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses的更多相关文章

  1. 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> ...

  2. Jersey(1.19.1) - Hello World, Get started with a Web application

    1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...

  3. Jersey(1.19.1) - JSON Support

    Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...

  4. Jersey(1.19.1) - Root Resource Classes

    Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...

  5. Jersey(1.19.1) - Deploying a RESTful Web Service

    JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and pro ...

  6. Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses

    Previous sections have shown how to return HTTP responses and it is possible to return HTTP errors u ...

  7. Jersey(1.19.1) - Life-cycle of Root Resource Classes

    By default the life-cycle of root resource classes is per-request, namely that a new instance of a r ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 使用C#读取XML节点,修改XML节点

    例子: <add key="IsEmptyDGAddRootText" value="" /> <!--是否可以修改归档状态档案 false: ...

  2. Spring Data JPA Tutorial Part Nine: Conclusions(未翻译)

    This is the ninth and the last part of my Spring Data JPA tutorial. Now it is time to take a look of ...

  3. 如何关闭dell inspiron n4010的内置麦克

    如何关闭dell inspiron n4010的内置麦克 dell inspiron n4010这款电脑的内置麦克是默认开启的,如果你的扩音器音量开得稍大,当你打字的时候就会听到回音,最讨厌的是,当你 ...

  4. Laravel入门笔记

    Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...

  5. Meta键盘

    由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...

  6. 测试URL有效性

    方法一: #禁用滚动条 $ProgressPreference='silentlycontinue' Invoke-WebRequest "www.163.com" -UseBas ...

  7. 内容输出Linux文件系统的的实现:创建一个文件的过程

    题记:写这篇博客要主是加深自己对内容输出的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 考虑上面这个命令: who > userlist 当这个命令完成后,文件系统增加l一 ...

  8. 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...

  9. HDU 4293 Groups (线性dp)

    OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...

  10. MySQL server version for the right syntax to use near ‘USING BTREE

    转自:http://www.douban.com/note/157818842/ 有时导入mysql会提示如下错误: C:\Users\liqiang>mysql -uroot -paaaaaa ...