Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions spring-boot/src/main/java/org/baeldung/config/LoggerConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
4 changes: 3 additions & 1 deletion spring-boot/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.mail.host=localhost
spring.mail.port=8025
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.auth=false

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG