向Spring控制器添​​加标头 [英] Adding headers to Spring controllers

查看:115
本文介绍了向Spring控制器添​​加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题非常类似于这个 one,但我觉得它的不同和具体到足以在这里保证自己的问题。



我刚刚从一个独立开发者那里继承了一个Java Web应用程序项目没有文件背后。它是一个具有基本包结构的Spring MVC应用程序,如下所示:

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

每个控制器类只是用 @Controller 注释的POJO和 @RequestMapping(/ blah) )。例如:

  @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){
//只要:
// http://ourSite.com/images执行此方法/ saveImage
// URL被点击
}
}

我被要求将以下HTTP标头添加到Spring配置中,以便我们禁用浏览器缓存:


Pragma:no-cache



缓存控制:无缓存



到期:-1




我上面链接的文章听起来好像我们的控制器应该扩展这个 WebContentGenerator 类。不幸的是,有许多控制器具有大量的方法,因此重构每个控制器以扩展或继承某些基本类型并不是一个可行的选择(除非选项!)。



我也看过一些文章,听起来你必须配置Spring来使用AOP拦截器修改你的响应标题,但现在我真的进入了一个不熟悉的领域。 / p>

鉴于我们对Spring MVC的设置和实现,我最简单的方法是将这三个简单的标题添加到服务器发回的每个响应中(无论哪个控制器或方法)执行)?



提前致谢!

解决方案

希望你使用Spring 3,您可以查看拦截器,然后您不必修改所有控制器(因为您说您有很多)。看起来他们可能已经实现了一个你可以使用的实现。查看Bozho对此问题的回答如何通过注释在弹簧mvc 3中设置标题无缓存


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!

解决方案

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

这篇关于向Spring控制器添​​加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆