6

I know this question is very similar to this one, but I feel its different and specific enough to warrant its own question here.

I've just inherited a Java web app project from a sole developer who left no documentation behind. Its a Spring MVC app with a basic package structure as follows:

com.ourOrg.app.controllers
    ImageController
    ProgramController
    UserController
com.ourOrg.app.otherPackages

Each Controller class is just a POJO annotated with @Controller and @RequestMapping("/blah"). For instance:

@Controller
@RequestMapping("/images")
public class ImageController() {
    @RequestMapping(value="/saveImage", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveImage(@RequestParam(value="imageData", required=true) String imageXML, HttpServletRequest request){
        // This method gets executed whenever the:
        // http://ourSite.com/images/saveImage
        // URL is hit
    }
}

I have been asked to add the following HTTP headers to the Spring config so that we disable browser caching:

Pragma: no-cache

Cache-Control: no-cache

Expires: -1

The article I linked to above makes it sound like our controllers should be extending this WebContentGenerator class. Unfortunately, there are dozens of controllers with an enormous number of methods, so refactoring each one to extend or inherit some base type is not really a viable option (unless its the only option!).

I've also seen articles that make it sound like you have to configure Spring to use AOP interceptors that modify your response headers, but now I'm really getting into unfamiliar territory.

Given our setup and implementation of Spring MVC, whats the easiest way for me to add these three simple headers to every response sent back by the server (regardless of which controller or method is executed)?

Thanks in advance!

Community
  • 1
  • 1
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • Perhaps I'm mistaken, but can't you set these in the HttpServletResponse object prior to exiting your controller? Your controllers should have access to that object. – jamesmortensen May 15 '12 at 19:47
  • How? Only the annotations are making them controllers, but they only extends `Object`... – IAmYourFaja May 15 '12 at 19:49

2 Answers2

3

Hoping you are using Spring 3, you can look at an interceptor, then you won't have to modify all of your controllers (since you said you had many). It looks like they may already have one implemented that you can just use. Check out Bozho's answer to this question how to set header no cache in spring mvc 3 by annotation

Community
  • 1
  • 1
digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • I think if you can add an example to this, yours is the better answer between yours and mine. Good idea on the Interceptor. +1 – jamesmortensen May 15 '12 at 19:55
  • I hesitate to really blow this out with an example because it's really just Bozho's answer, so I'd rather people go to that question and upvote his answer since he earned it. – digitaljoel May 15 '12 at 19:57
  • @digitaljoel - So I just add this XML into my Spring config and Spring will know to intercept all HTTP responses and inject them with these caching headers? Do I need to do any "AOP stuff" (defining pointcuts, etc.)? – IAmYourFaja May 15 '12 at 19:59
  • You don't need to configure any AOP stuff. It's like a filter but handled in spring. This article might help you understand the role of an Interceptor http://java.dzone.com/articles/using-spring-interceptors-your The xml in Bozho's answer just configures an existing interceptor instead of you having to create your own. – digitaljoel May 15 '12 at 20:13
  • Thanks - last question - will this code override or append to the headers currently being returned? In other words, will it "blow out" the HTTP response headers currently coming back from the server? – IAmYourFaja May 15 '12 at 21:01
  • I've never used it, but I suspect it would "play nice" and only override the headers it is concerned with rather than blowing away existing stuff. When you try it perhaps you could let us know what you find out. – digitaljoel May 15 '12 at 21:35
-1

I realize this is an old post but maybe this will help somebody. I am using Spring for this example.

The main thing is use the annotation for parameters:

@Controller
public class HelloController {
 
    @RequestMapping(value = "/hello.htm")
    public String hello(@RequestHeader(value="User-Agent") String userAgent)
 
        //..
    }
}
рüффп
  • 5,172
  • 34
  • 67
  • 113
Pomagranite
  • 696
  • 5
  • 11