From af596b23381616097aa132ef2eacfcd3eeaad02e Mon Sep 17 00:00:00 2001 From: chirag d Date: Sat, 15 Feb 2020 21:57:54 +0530 Subject: [PATCH] BAEL:3771 Few review comments --- .../cache/CacheControlController.java | 21 +++++++++---------- .../java/com/baeldung/cache/WebConfig.java | 2 +- ...CacheControlControllerIntegrationTest.java | 7 ++++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java index 5bf095888e..7305f836cf 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java @@ -3,9 +3,8 @@ package com.baeldung.cache; import org.springframework.http.CacheControl; import org.springframework.http.ResponseEntity; 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.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.context.request.WebRequest; import javax.servlet.http.HttpServletResponse; @@ -16,28 +15,28 @@ import java.util.concurrent.TimeUnit; @Controller public class CacheControlController { - @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) - public ResponseEntity helloWorld(@PathVariable String name) { + @GetMapping(value = "/hello/{name}") + public ResponseEntity hello(@PathVariable String name) { CacheControl cacheControl = CacheControl.maxAge(60, TimeUnit.SECONDS) - .noTransform() - .mustRevalidate(); + .noTransform() + .mustRevalidate(); return ResponseEntity.ok() - .cacheControl(cacheControl) - .body("Hello " + name); + .cacheControl(cacheControl) + .body("Hello " + name); } - @RequestMapping(value = "/home/{name}", method = RequestMethod.GET) + @GetMapping(value = "/home/{name}") public String home(@PathVariable String name, final HttpServletResponse response) { response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); return "home"; } - @RequestMapping(value = "/cache/{name}", method = RequestMethod.GET) + @GetMapping(value = "/login/{name}") public ResponseEntity intercept(@PathVariable String name) { return ResponseEntity.ok().body("Hello " + name); } - @RequestMapping(value = "/validate/{name}", method = RequestMethod.GET) + @GetMapping(value = "/productInfo/{name}") public ResponseEntity validate(@PathVariable String name, WebRequest request) { ZoneId zoneId = ZoneId.of("GMT"); diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java index 728479979c..2f07912e80 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java @@ -35,7 +35,7 @@ public class WebConfig implements WebMvcConfigurer { WebContentInterceptor interceptor = new WebContentInterceptor(); interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS) .noTransform() - .mustRevalidate(), "/cache/*"); + .mustRevalidate(), "/login/*"); registry.addInterceptor(interceptor); } } \ No newline at end of file diff --git a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java index ef408038ae..7acfe5e480 100644 --- a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java @@ -37,7 +37,8 @@ public class CacheControlControllerIntegrationTest { this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/baeldung")) .andDo(MockMvcResultHandlers.print()) .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 @@ -59,7 +60,7 @@ public class CacheControlControllerIntegrationTest { @Test void whenInterceptor_thenReturnCacheHeader() throws Exception { - this.mockMvc.perform(MockMvcRequestBuilders.get("/cache/baeldung")) + this.mockMvc.perform(MockMvcRequestBuilders.get("/login/baeldung")) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.status().isOk()) .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 { HttpHeaders headers = new HttpHeaders(); 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()) .andExpect(MockMvcResultMatchers.status().is(304)); }