如何防止spring boot中的缓存

时间:2016-08-24 09:39:35

标签: caching spring-boot

我有一个gradle项目,我在后端使用spring-boot和前端Angular 2。 每次我在添加或删除后执行get-Request时,IE都不会调用get-method。他从缓存中获取数组。

我已经找到了解决方案客户端。我已经把标题:

=DataSources("DataSource1").DataSourceReference

在我的请求中。然后就行了。

但我试图在服务器端解决这个问题。我在互联网上发现,如果你只是添加

,你可以阻止缓存
        'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': '0'
在您的application.properties中

。但这不起作用。

我忘了别的什么吗?

应用:

spring.cache.type=NONE

UIController:

@SpringBootApplication
public class MyApplicationextends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);

    }
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

RestController:

@RequestMapping("/service")
public interface MyUIController {

    @RequestMapping(method=RequestMethod.GET, value= "/getBooks", produces="application/json")
    public List<Books> getBooks(HttpServletRequest request,  HttpServletResponse response);
}

服务:

@RestController
public class MyRestController implements MyUIController {
  public List<DomainEntity> getDomains(HttpServletRequest request, HttpServletResponse response) {
return myUIService.getBookss(request, response);

1 个答案:

答案 0 :(得分:0)

正如M. Deinum所提到的,spring.cache.type用于缓存支持,这完全是另一个弹簧特征。

但是根据Spring docs,可以使用此java配置来启用缓存控制:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        // ...
        .headers().cacheControl();
    }
}

这将自动添加必要的缓存控制标头。