Hi,
You do not need to use an input stream for the second case you can do:
return Response.ok(new GenericEntity<List<AlertData>>(list) {},
"application/xml”).
cacheControl(cacheControl).build();
but your general point still holds, and i know the above sucks because
we failed to make Response generic.
You can use a filter:
https://jersey.dev.java.net/nonav/apidocs/1.2-SNAPSHOT/jersey/com/sun/jersey/api/container/filter/package-summary.html
If you want to set the cache control header for all resources you can
use a response filter. If you want to set the cache control header for
specific resources say by declaring your own annotation, e.g.
@CacheControl, on a resource method you can use a resource filter that
only applies a response filter if @CacheControl is present on a
resource method.
Paul.
On Mar 24, 2010, at 5:37 AM, Suchitha Koneru (sukoneru) wrote:
> Hello Jersey Users,
> We are using Jersey version 1.1.1-ea . Can we set
> headers on response of an API without returning a response object as
> shown below. We want to make sure that consumer of restful services
> does not cache the content.
>
> @GET
> @Produces ("application/xml")
> @Path ("/getNewAlertData/{customerId}/{alertId}")
> public List<AlertData>
> getNewAlertData(@PathParam("customerId") int
> customerId,_at_PathParam("alertId") longalertId) {
> List<AlertData> list = null;
>
>
> try {
>
>
> AlertReportSessionBeanLocal sessionBean =
> (AlertReportSessionBeanLocal)
> JNDIServiceLocator.getInstance().getJndiService("alerts/
> AlertReportSessionBean/local");
> list = sessionBean.getNewAlertData(customerId,
> alertId);
>
> CacheControl cacheControl = new CacheControl();
> cacheControl.setMaxAge(0);
>
> Response.ResponseBuilder builder = Response.ok();
> builder.cacheControl(cacheControl);
>
>
> } catch (Exception NamingException) {
> NamingException.printStackTrace();
> throw new NotFoundException("Invalid request");
> }
>
> return list;
> }
>
>
> We tried the above code and it did not work. We do not want to
> change the signature of the API and the code such that it returns a
> response object
>
> i.e.
>
> public Response getNewAlertData(@PathParam("customerId") int
> customerId,_at_PathParam("alertId") long alertId) {
>
> try {
>
>
>
> AlertReportSessionBeanLocal sessionBean =
> (AlertReportSessionBeanLocal)
> JNDIServiceLocator.getInstance().getJndiService("alerts/
> AlertReportSessionBean/local");
> list = sessionBean.getNewAlertData(customerId,
> alertId);
>
> InputStream is = getStream(list);
>
> CacheControl cacheControl = new CacheControl();
> cacheControl.setMaxAge(0);
>
> Response.ResponseBuilder builder = Response.ok(is, "application/xml”);
> builder.cacheControl(cacheControl);
>
>
> } catch (Exception NamingException) {
> NamingException.printStackTrace();
> throw new NotFoundException("Invalid request");
> }
>
> return builder.build();
>
>
> }
>
> Is there a way to achieve this without returning a response
> object ? Can we set headers at the service level rather than at
> the API level , so that
> All the apis in the restful service can use the same headers. Could
> you please let me know,
>
> Thanks,
> Suchitha
>
>
>