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. WordPress 主题框架是如何工作的

    主题框架可以说是无比强大的!对于非技术型的 WordPress 用户来说,主题框架使得建立一个独一无二并看起来像是运行一个量身定制的主题的网站成为可能,并且对于 WordPress 开发者来说,它们能 ...

  2. IoC模式(Inversion of Control)

      1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它.下面看 ...

  3. ibatis 搭建总结

    一.搭建ibatis环境 1.导入ibatis的jar包,已及数据库驱动jar包ibatis-2.3.0.677.jar ibatis-dao-2.jar ibatis-sqlmap-2.jar ib ...

  4. .NET下的延迟加载

    在应用中有很多实例可能需要延迟创建对象, 比如设计模式中的单例模式就是一种非常常见的情况.如果不考虑线程安全我们通常会编写如下代码: public class SingleInstance { pri ...

  5. Apache POI 合并单元格

    合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress  cellRangeAddress  );   CellRangeAddress  对象的构造 ...

  6. 关于webapi 返回的类型的笔记

    经过测试发现使用IE浏览器返回的数据是json,而使用Firefox和Chrome返回的则为xml,经研究发现IE在发生http请求时请求头accpet节点相比Firefox和Chrome缺少&quo ...

  7. delphi Components[i]清除所有edit控件中的内容

    (* 一般的清空combobox方法 combobox1.clear; ... combobox9.clear; *)   procedure TForm1.Button1Click(Sender: ...

  8. [Javascript] Manipulate the DOM with the classList API

    Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than u ...

  9. Blueprint编译过程

    Blueprint 编译概述 一.术语 Blueprint,像C++语言一下的,在游戏中使用前须要编译.当你在BP编辑器中,点击编译button时候.BP资源開始把属性和图例过程转换为一个类对象处理. ...

  10. 简简单单安装debian桌面工作环境

    linux一般给人的影响是对使用者的要求偏高, 使用者需要自行配置很多相应的系统工作参数,因此,从一定的程度上阻碍了用户去使用它.而本文所介绍的是, 使用者完全可以消除这个障碍,非常简单地安装好自己的 ...