Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:01:05+00:00 2026-06-05T13:01:05+00:00

While writing a RESTful web service, I am encountering issues if I enable any

  • 0

While writing a RESTful web service, I am encountering issues if I enable any sort of caching on my client (currently a .NET thick client). By default Jersey is not sending any sort of cache control header, so the client is caching most pages automatically (which seems to be valid behaviour).

I would like to have Jersey by default send a cache control of “no-cache”, and then in particular responses override the cache control.

Is there any way to do this with Jersey?

I’ve found that RESTeasy has the ability to use the @NoCache annotation to specify the setting for the whole class, but I’ve not found anything similar with Jersey.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:01:08+00:00Added an answer on June 5, 2026 at 1:01 pm

    This is easy with Jersey by using a ResourceFilterFactory – you can create any custom annotation you attach to your methods to set cache control settings. ResourceFilterFactories get called for each discovered resource method when the application initializes – in your ResourceFilterFactory you can check if the method has your @CacheControlHeader annotation (or whatever you want to call it) – if not, simply return response filter that adds “no-cache” directive to the response, otherwise it should use the settings from the annotation. Here is an example of how to do that:

    public class CacheFilterFactory implements ResourceFilterFactory {
        private static final List<ResourceFilter> NO_CACHE_FILTER = Collections.<ResourceFilter>singletonList(new CacheResponseFilter("no-cache"));
    
        @Override
        public List<ResourceFilter> create(AbstractMethod am) {
            CacheControlHeader cch = am.getAnnotation(CacheControlHeader.class);
            if (cch == null) {
                return NO_CACHE_FILTER;
            } else {
                return Collections.<ResourceFilter>singletonList(new CacheResponseFilter(cch.value()));
            }
        }
    
        private static class CacheResponseFilter implements ResourceFilter, ContainerResponseFilter {
            private final String headerValue;
    
            CacheResponseFilter(String headerValue) {
                this.headerValue = headerValue;
            }
    
            @Override
            public ContainerRequestFilter getRequestFilter() {
                return null;
            }
    
            @Override
            public ContainerResponseFilter getResponseFilter() {
                return this;
            }
    
            @Override
            public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
                // attache Cache Control header to each response based on the annotation value
                response.getHttpHeaders().putSingle(HttpHeaders.CACHE_CONTROL, headerValue);
                return response;
            }
        }
    }
    

    The annotation can look like this:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface CacheControlHeader {
        String value();
    }
    

    The ResourceFilterFactory can be registered in your application by adding the following init param to the definition of Jersey servlet in web.xml:

    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>package.name.CacheFilterFactory</param-value>
    </init-param>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing a login system for a web project Im working on I came
While writing a simple server-client application, this question came in my mind. When someone
Any solution for reducing browser compatibility problem while writing css style.
While writing a web application, it makes sense to store (server side) all datetimes
While writing some aysnc controllers in ASP.NET MVC2, I ran up against a situation
While writing and testing a python method, I am currently using the following approach:
while writing a custom attribute in C# i was wondering if there are any
While writing an asp.net project i may have this url for testing/debugging http://localhost:1234/ I
I have a RESTful web service with a C++ API at the back-end. I
While writing an encryption method in JavaScript, I came to wondering what character encoding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.