From 00dd88a0b8eb6be382c7dfdde874af87dc54b6c9 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Mon, 20 Jul 2015 13:05:25 +0200 Subject: [PATCH 1/5] Redirect in Spring --- spring-rest/README.md | 2 +- .../redirect/RedirectController.java | 52 ++++++++++++++++ .../redirect/RedirectControllerTest.java | 61 +++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java create mode 100644 spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java diff --git a/spring-rest/README.md b/spring-rest/README.md index 5e0a62c3eb..3231d65625 100644 --- a/spring-rest/README.md +++ b/spring-rest/README.md @@ -6,4 +6,4 @@ ### Relevant Articles: - [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) - [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest) - +- [Redirect in Spring](http://www.baeldung.com/spring-redirect) diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java new file mode 100644 index 0000000000..e687f76e62 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -0,0 +1,52 @@ +package org.baeldung.redirect; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +@RequestMapping("/") +public class RedirectController { + + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } +} \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java new file mode 100644 index 0000000000..9b16891336 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java @@ -0,0 +1,61 @@ +package org.baeldung.redirect; + +import static org.hamcrest.CoreMatchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") +public class RedirectControllerTest { + + private MockMvc mockMvc; + + @Autowired + protected WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = webAppContextSetup(this.wac).build(); + } + + @Test + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + this.mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + this.mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + this.mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + this.mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + } + + @Test + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { + this.mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + } + +} From c4bc953d1c1288c6bf634bdecc5b0cd2587e1742 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Tue, 21 Jul 2015 13:06:14 +0200 Subject: [PATCH 2/5] Fix issues with tests. --- spring-rest/pom.xml | 11 ++- .../redirect/RedirectController.java | 64 +++++++-------- .../src/main/webapp/WEB-INF/api-servlet.xml | 20 ++++- .../src/main/webapp/WEB-INF/spring-views.xml | 10 +++ .../redirect/RedirectControllerTest.java | 82 +++++++++++-------- 5 files changed, 113 insertions(+), 74 deletions(-) create mode 100644 spring-rest/src/main/webapp/WEB-INF/spring-views.xml diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 542fa1d025..ecfe1f40f0 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -153,6 +153,13 @@ ${mockito.version} test + + + org.springframework + spring-test + ${spring.version} + + @@ -172,8 +179,8 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - 1.8 - 1.8 + 1.7 + 1.7 diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java index e687f76e62..db83495c6f 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -1,4 +1,4 @@ -package org.baeldung.redirect; +package org.baeldung.web.controller.redirect; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; @@ -13,40 +13,40 @@ import org.springframework.web.servlet.view.RedirectView; @RequestMapping("/") public class RedirectController { - @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) - public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { - model.addAttribute("attribute", "redirectWithXMLConfig"); - return new ModelAndView("RedirectedUrl", model); - } + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectPrefix"); - return new ModelAndView("redirect:/redirectedUrl", model); - } + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) - public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { - redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); - redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) - public RedirectView redirectWithUsingRedirectView(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectView"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithForwardPrefix"); - return new ModelAndView("forward:/redirectedUrl", model); - } + @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) - public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { - model.addAttribute("redirectionAttribute", flashAttribute); - return new ModelAndView("redirection", model); - } + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } } \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml index acab871c88..2e01bb636b 100644 --- a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml +++ b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml @@ -1,9 +1,20 @@ - + - + + + + + + + /WEB-INF/spring-views.xml + + + + + @@ -13,4 +24,5 @@ + \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/spring-views.xml b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml new file mode 100644 index 0000000000..83c7828d9a --- /dev/null +++ b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java index 9b16891336..12ac394f90 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java @@ -1,9 +1,15 @@ -package org.baeldung.redirect; +package org.baeldung.web.controller.redirect; -import static org.hamcrest.CoreMatchers.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import org.junit.Before; import org.junit.Test; @@ -16,46 +22,50 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") @WebAppConfiguration -@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") public class RedirectControllerTest { - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - protected WebApplicationContext wac; + @Autowired + protected WebApplicationContext wac; - @Before - public void setup() { - this.mockMvc = webAppContextSetup(this.wac).build(); - } + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } - @Test - public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); - } + @Test + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()) + .andExpect(view().name("RedirectedUrl")) + .andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) - .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) - .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("flashAttribute", is(nullValue()))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + } - @Test - public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); - } + @Test + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + } } From 877b8d2987b962ba023d8391a47d6b4f1cfec919 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Tue, 21 Jul 2015 15:07:32 +0200 Subject: [PATCH 3/5] Rename some urls. --- .../redirect/RedirectController.java | 7 ++-- .../src/main/webapp/WEB-INF/api-servlet.xml | 33 ++++++++++--------- .../redirect/RedirectControllerTest.java | 22 +++++++++---- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java index db83495c6f..1b87ab2816 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -38,14 +38,15 @@ public class RedirectController { return new RedirectView("redirectedUrl"); } - @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { + @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { model.addAttribute("attribute", "redirectWithForwardPrefix"); return new ModelAndView("forward:/redirectedUrl", model); } @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) - public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + public ModelAndView redirection(final ModelMap model, + @ModelAttribute("flashAttribute") final Object flashAttribute) { model.addAttribute("redirectionAttribute", flashAttribute); return new ModelAndView("redirection", model); } diff --git a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml index 2e01bb636b..9bcff1dfe3 100644 --- a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml +++ b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml @@ -1,10 +1,13 @@ - + - + + + - - @@ -14,15 +17,15 @@ - - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java index 12ac394f90..c1ce21bef1 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java @@ -46,26 +46,36 @@ public class RedirectControllerTest { @Test public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()) + .andExpect(view().name("redirect:/redirectedUrl")) + .andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); } @Test - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() + throws Exception { + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()) + .andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))) .andExpect(model().attribute("flashAttribute", is(nullValue()))) .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); } @Test - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() + throws Exception { + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()) + .andExpect(model().attribute("attribute", is("redirectWithRedirectView"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); } @Test public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()) + .andExpect(view().name("forward:/redirectedUrl")) + .andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))) + .andExpect(forwardedUrl("/redirectedUrl")); } } From 23bf5a304249b438acf80ca30a0b2a9bf6c41648 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Wed, 22 Jul 2015 22:22:39 +0200 Subject: [PATCH 4/5] Fix formatting files --- .../redirect/RedirectController.java | 63 ++++++++-------- .../src/main/webapp/WEB-INF/api-servlet.xml | 46 ++++++------ .../src/main/webapp/WEB-INF/spring-views.xml | 14 ++-- spring-rest/src/main/webapp/WEB-INF/web.xml | 63 ++++++++-------- .../redirect/RedirectControllerTest.java | 74 ++++++++----------- 5 files changed, 123 insertions(+), 137 deletions(-) diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java index 1b87ab2816..472c0c8bf5 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -13,41 +13,40 @@ import org.springframework.web.servlet.view.RedirectView; @RequestMapping("/") public class RedirectController { - @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) - public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { - model.addAttribute("attribute", "redirectWithXMLConfig"); - return new ModelAndView("RedirectedUrl", model); - } + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectPrefix"); - return new ModelAndView("redirect:/redirectedUrl", model); - } + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) - public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { - redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); - redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) - public RedirectView redirectWithUsingRedirectView(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectView"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) - public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithForwardPrefix"); - return new ModelAndView("forward:/redirectedUrl", model); - } + @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) - public ModelAndView redirection(final ModelMap model, - @ModelAttribute("flashAttribute") final Object flashAttribute) { - model.addAttribute("redirectionAttribute", flashAttribute); - return new ModelAndView("redirection", model); - } + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } } \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml index 9bcff1dfe3..5afc637ece 100644 --- a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml +++ b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml @@ -1,31 +1,33 @@ - + - + - + - - - - /WEB-INF/spring-views.xml - - - + + + + /WEB-INF/spring-views.xml + + + - - - + + + - - + + - + + + + - \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/spring-views.xml b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml index 83c7828d9a..2944828d6d 100644 --- a/spring-rest/src/main/webapp/WEB-INF/spring-views.xml +++ b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml @@ -1,10 +1,10 @@ - + - - - + + + \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/web.xml b/spring-rest/src/main/webapp/WEB-INF/web.xml index 48d4b8fe61..c28bb9a20a 100644 --- a/spring-rest/src/main/webapp/WEB-INF/web.xml +++ b/spring-rest/src/main/webapp/WEB-INF/web.xml @@ -1,42 +1,41 @@ - + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" +> - Spring MVC Application + Spring MVC Application - - - contextClass - + + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - contextConfigLocation - org.baeldung.config - + + + contextConfigLocation + org.baeldung.config + - - org.springframework.web.context.ContextLoaderListener - + + org.springframework.web.context.ContextLoaderListener + - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + - - - + + + - \ No newline at end of file + diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java index c1ce21bef1..ab2b9f3f87 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java @@ -26,56 +26,42 @@ import org.springframework.web.context.WebApplicationContext; @WebAppConfiguration public class RedirectControllerTest { - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - protected WebApplicationContext wac; + @Autowired + protected WebApplicationContext wac; - @Before - public void setup() { - mockMvc = webAppContextSetup(wac).build(); - } + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } - @Test - public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("RedirectedUrl")) - .andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); - } + @Test + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/redirectedUrl")) - .andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) - .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() - throws Exception { - mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()) - .andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) - .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))) - .andExpect(model().attribute("flashAttribute", is(nullValue()))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() - throws Exception { - mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()) - .andExpect(model().attribute("attribute", is("redirectWithRedirectView"))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + } - @Test - public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { - mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()) - .andExpect(view().name("forward:/redirectedUrl")) - .andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))) - .andExpect(forwardedUrl("/redirectedUrl")); - } + @Test + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { + mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + } } From b3d9ada21eed404e90a4a2a7dc9acf9f940dcd16 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Wed, 22 Jul 2015 23:01:00 +0200 Subject: [PATCH 5/5] Fix formatting for pom --- spring-rest/pom.xml | 493 ++++++++++++++++++++++---------------------- 1 file changed, 246 insertions(+), 247 deletions(-) diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index ecfe1f40f0..abc28ace44 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -1,274 +1,273 @@ - - 4.0.0 - org.baeldung - spring-rest - 0.1-SNAPSHOT - spring-rest - war + + 4.0.0 + org.baeldung + spring-rest + 0.1-SNAPSHOT + spring-rest + war - - org.springframework.boot - spring-boot-starter-parent - 1.2.4.RELEASE - + + org.springframework.boot + spring-boot-starter-parent + 1.2.4.RELEASE + - + - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-actuator - - - - - - org.springframework - spring-web - ${org.springframework.version} - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - ${org.springframework.version} - - - org.springframework - spring-oxm - ${org.springframework.version} - - - - commons-fileupload - commons-fileupload - 1.3.1 - - - - - javax.servlet - javax.servlet-api - 3.0.1 - provided - - - - javax.servlet - jstl - runtime - - - - - - com.fasterxml.jackson.core - jackson-databind - - - - com.thoughtworks.xstream - xstream - 1.4.8 - - - - - - com.google.guava - guava - ${guava.version} - - - - org.apache.commons - commons-lang3 - 3.2.1 - - - - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - - - - junit - junit-dep - ${junit.version} - test - - - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - - - - org.mockito - mockito-core - ${mockito.version} - test - - - - org.springframework - spring-test - ${spring.version} + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-actuator - - + - - spring-rest - - - src/main/resources - true - - + + org.springframework + spring-web + ${org.springframework.version} + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + org.springframework + spring-oxm + ${org.springframework.version} + - + + commons-fileupload + commons-fileupload + 1.3.1 + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + + javax.servlet + javax.servlet-api + 3.0.1 + provided + - - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - + + javax.servlet + jstl + runtime + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - - - - - - + - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - - 8082 - - - - + + com.fasterxml.jackson.core + jackson-databind + - + + com.thoughtworks.xstream + xstream + 1.4.8 + - + - - - 4.1.6.RELEASE - 3.2.7.RELEASE + + com.google.guava + guava + ${guava.version} + - - 4.3.10.Final - 5.1.35 + + org.apache.commons + commons-lang3 + 3.2.1 + - + - 2.5.1 + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + - - 5.1.3.Final + - - 18.0 - 3.4 + + junit + junit-dep + ${junit.version} + test + - - 1.3 - 4.11 - 1.10.19 + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - 4.4.1 - 4.5 + + org.mockito + mockito-core + ${mockito.version} + test + - 2.4.1 + + org.springframework + spring-test + ${spring.version} + - - 1.7.12 - 1.1.3 - - 3.3 - 2.6 - 2.18.1 - 1.4.14 + - + + spring-rest + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + + + + + + + + + 4.1.6.RELEASE + 3.2.7.RELEASE + + + 4.3.10.Final + 5.1.35 + + + + 2.5.1 + + + 5.1.3.Final + + + 18.0 + 3.4 + + + 1.3 + 4.11 + 1.10.19 + + 4.4.1 + 4.5 + + 2.4.1 + + + 1.7.12 + 1.1.3 + + + 3.3 + 2.6 + 2.18.1 + 1.4.14 + + \ No newline at end of file