Conditional GETs is a great a-to-reduce bandwidth, and potentially server-side performance, depending on how the inform Ation 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:
PublicSparklinesresource (@QueryParam ("D") integerlist data, @DefaultValue ("0,100") @QueryParam ("Limits")) Interval limits, @Context request request, @Context Uriinfo UI) {if(Data = =NULL) Throw NewWebapplicationexception (400); This. data =data; This. Limits =limits; if(!limits.contains (data))Throw NewWebapplicationexception (400); This. Tag =Computeentitytag (Ui.getrequesturi ()); if(Request.getmethod (). Equals ("GET") {Response.responsebuilder RB=request.evaluatepreconditions (tag); if(RB! =NULL) Throw Newwebapplicationexception (Rb.build ()); }}
The constructor of the SparklinesResouce
root resource class computes an entity tag from the request URI and then calls the Request.ev Aluatepreconditions with that entity tag. If A client request contains an If-None-Match
header with a value that contains the same entity tag that is calculated then the E Valuatepreconditionsreturns a pre-filled out response, with the 304 status code and entity tag set, which may be built and Returned. Otherwise, Evaluatepreconditions returns and the null
normal response can be returned.
Notice That's example the constructor of a resource class can be used perform actions which may otherwise has to be Duplicated to invoked for each resource method.
Jersey (1.19.1)-Conditional GETs and returning 304 (not Modified) responses