BAEL3771 Review Comments (#8726)

* BAEL3771: Cache Headers in Spring MVC

* BEAL3771: Cache Headers in Spring MVC

* BEAL3771: Cache Headers in Spring MVC

* BAEL:3771 Few review comments
This commit is contained in:
dev-chirag 2020-02-15 22:12:27 +05:30 committed by GitHub
parent 9e421b1247
commit 49cda16d69
3 changed files with 15 additions and 15 deletions

View File

@ -3,9 +3,8 @@ package com.baeldung.cache;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -16,28 +15,28 @@ import java.util.concurrent.TimeUnit;
@Controller @Controller
public class CacheControlController { public class CacheControlController {
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) @GetMapping(value = "/hello/{name}")
public ResponseEntity<String> helloWorld(@PathVariable String name) { public ResponseEntity<String> hello(@PathVariable String name) {
CacheControl cacheControl = CacheControl.maxAge(60, TimeUnit.SECONDS) CacheControl cacheControl = CacheControl.maxAge(60, TimeUnit.SECONDS)
.noTransform() .noTransform()
.mustRevalidate(); .mustRevalidate();
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(cacheControl) .cacheControl(cacheControl)
.body("Hello " + name); .body("Hello " + name);
} }
@RequestMapping(value = "/home/{name}", method = RequestMethod.GET) @GetMapping(value = "/home/{name}")
public String home(@PathVariable String name, final HttpServletResponse response) { public String home(@PathVariable String name, final HttpServletResponse response) {
response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform");
return "home"; return "home";
} }
@RequestMapping(value = "/cache/{name}", method = RequestMethod.GET) @GetMapping(value = "/login/{name}")
public ResponseEntity<String> intercept(@PathVariable String name) { public ResponseEntity<String> intercept(@PathVariable String name) {
return ResponseEntity.ok().body("Hello " + name); return ResponseEntity.ok().body("Hello " + name);
} }
@RequestMapping(value = "/validate/{name}", method = RequestMethod.GET) @GetMapping(value = "/productInfo/{name}")
public ResponseEntity<String> validate(@PathVariable String name, WebRequest request) { public ResponseEntity<String> validate(@PathVariable String name, WebRequest request) {
ZoneId zoneId = ZoneId.of("GMT"); ZoneId zoneId = ZoneId.of("GMT");

View File

@ -35,7 +35,7 @@ public class WebConfig implements WebMvcConfigurer {
WebContentInterceptor interceptor = new WebContentInterceptor(); WebContentInterceptor interceptor = new WebContentInterceptor();
interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS) interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS)
.noTransform() .noTransform()
.mustRevalidate(), "/cache/*"); .mustRevalidate(), "/login/*");
registry.addInterceptor(interceptor); registry.addInterceptor(interceptor);
} }
} }

View File

@ -37,7 +37,8 @@ public class CacheControlControllerIntegrationTest {
this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/baeldung")) this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/baeldung"))
.andDo(MockMvcResultHandlers.print()) .andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.header().string("Cache-Control","max-age=60, must-revalidate, no-transform")); .andExpect(MockMvcResultMatchers.header()
.string("Cache-Control","max-age=60, must-revalidate, no-transform"));
} }
@Test @Test
@ -59,7 +60,7 @@ public class CacheControlControllerIntegrationTest {
@Test @Test
void whenInterceptor_thenReturnCacheHeader() throws Exception { void whenInterceptor_thenReturnCacheHeader() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/cache/baeldung")) this.mockMvc.perform(MockMvcRequestBuilders.get("/login/baeldung"))
.andDo(MockMvcResultHandlers.print()) .andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.header().string("Cache-Control","max-age=60, must-revalidate, no-transform")); .andExpect(MockMvcResultMatchers.header().string("Cache-Control","max-age=60, must-revalidate, no-transform"));
@ -69,7 +70,7 @@ public class CacheControlControllerIntegrationTest {
void whenValidate_thenReturnCacheHeader() throws Exception { void whenValidate_thenReturnCacheHeader() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add(IF_UNMODIFIED_SINCE, "Tue, 04 Feb 2020 19:57:25 GMT"); headers.add(IF_UNMODIFIED_SINCE, "Tue, 04 Feb 2020 19:57:25 GMT");
this.mockMvc.perform(MockMvcRequestBuilders.get("/validate/baeldung").headers(headers)) this.mockMvc.perform(MockMvcRequestBuilders.get("/productInfo/baeldung").headers(headers))
.andDo(MockMvcResultHandlers.print()) .andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().is(304)); .andExpect(MockMvcResultMatchers.status().is(304));
} }