* BAEL3771: Cache Headers in Spring MVC * BEAL3771: Cache Headers in Spring MVC * BEAL3771: Cache Headers in Spring MVC
41 lines
1.6 KiB
Java
41 lines
1.6 KiB
Java
package com.baeldung.cache;
|
|
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.CacheControl;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
import org.springframework.web.servlet.mvc.WebContentInterceptor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
@EnableWebMvc
|
|
@Configuration
|
|
@ComponentScan(basePackages = {"com.baeldung.cache"})
|
|
public class WebConfig implements WebMvcConfigurer {
|
|
|
|
@Override
|
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
|
registry.addViewController("/").setViewName("index");
|
|
}
|
|
|
|
@Override
|
|
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
|
|
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/")
|
|
.setCacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)
|
|
.noTransform()
|
|
.mustRevalidate());
|
|
}
|
|
|
|
@Override
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
WebContentInterceptor interceptor = new WebContentInterceptor();
|
|
interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS)
|
|
.noTransform()
|
|
.mustRevalidate(), "/cache/*");
|
|
registry.addInterceptor(interceptor);
|
|
}
|
|
} |