diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index b6a24b6cb7d6..b25933e75f67 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -86,6 +86,11 @@ json-path test + + com.google.code.gson + gson + 2.3 + org.springframework.boot spring-boot-starter-mail diff --git a/spring-boot/src/main/java/org/baeldung/config/LoggerConfig.java b/spring-boot/src/main/java/org/baeldung/config/LoggerConfig.java new file mode 100644 index 000000000000..a22fcfdb0350 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/config/LoggerConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@Configuration +public class LoggerConfig { + + @Bean + public CommonsRequestLoggingFilter requestLoggingFilter() { + CommonsRequestLoggingFilter crlf = new CommonsRequestLoggingFilter(); + crlf.setIncludeClientInfo(true); + crlf.setIncludeQueryString(true); + crlf.setIncludePayload(true); + crlf.setBeforeMessagePrefix("Before Commons Request Logging--"); + crlf.setAfterMessagePrefix("After Commons Request Logging--"); + return crlf; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/controller/HelloController.java b/spring-boot/src/main/java/org/baeldung/controller/HelloController.java new file mode 100644 index 000000000000..7dc572f9dd45 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/controller/HelloController.java @@ -0,0 +1,20 @@ +package org.baeldung.controller; + +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + @RequestMapping("/helloGet") + public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) { + return "hello"; + } + + @RequestMapping(value = "/helloPost", method = RequestMethod.POST) + public String helloPost(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) { + return "helloPost"; + } +} diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootRequestLoggingTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootRequestLoggingTest.java new file mode 100644 index 000000000000..2566e0af8750 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/SpringBootRequestLoggingTest.java @@ -0,0 +1,49 @@ +package org.baeldung; + +import java.nio.charset.Charset; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +@WebAppConfiguration +public class SpringBootRequestLoggingTest { + @Autowired + private WebApplicationContext webApplicationContext; + private MockMvc mockMvc; + + @Before + public void setupMockMvc() { + MockMvcBuilders.webAppContextSetup(webApplicationContext); + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).addFilter(webApplicationContext.getBean(CommonsRequestLoggingFilter.class)).build(); + + } + + @Test + public void givenRequestHasBeenMade_whenGetMeetsAllOfGivenConditions_thenCorrect() throws Exception { + MediaType contentType = new MediaType(MediaType.APPLICATION_FORM_URLENCODED.getType(), MediaType.APPLICATION_FORM_URLENCODED.getSubtype(), Charset.forName("utf8")); + + mockMvc.perform(MockMvcRequestBuilders.post("/helloGet?home=Houston").contentType(contentType)).andExpect(MockMvcResultMatchers.status().isOk()); + } + + @Test + public void givenRequestHasBeenMade_whenPostMeetsAllOfGivenConditions_thenCorrect() throws Exception { + MediaType contentType = new MediaType(MediaType.APPLICATION_FORM_URLENCODED.getType(), MediaType.APPLICATION_FORM_URLENCODED.getSubtype(), Charset.forName("utf8")); + + mockMvc.perform(MockMvcRequestBuilders.post("/helloPost").param("name", "suse").contentType(contentType)).andExpect(MockMvcResultMatchers.status().isOk()); + } + +} diff --git a/spring-boot/src/test/resources/application.properties b/spring-boot/src/test/resources/application.properties index 14b190629e16..86184aee8645 100644 --- a/spring-boot/src/test/resources/application.properties +++ b/spring-boot/src/test/resources/application.properties @@ -1,3 +1,5 @@ spring.mail.host=localhost spring.mail.port=8025 -spring.mail.properties.mail.smtp.auth=false \ No newline at end of file +spring.mail.properties.mail.smtp.auth=false + +logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG