diff --git a/.gitignore b/.gitignore index 248cbeae5d..90daf38dee 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,7 @@ out/ .DS_Store # Maven -log/ +log/* target/ # Gradle diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 555eafe40a..93160a9936 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -150,6 +150,11 @@ io.rest-assured rest-assured + + org.apache.httpcomponents + httpclient + ${httpclient.version} + @@ -276,6 +281,7 @@ 2.2.0 3.5.11 3.1.0 + 4.5.2 com.baeldung.sampleapp.config.MainApplication diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java index e9d451b55e..6e2607339c 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java @@ -1,9 +1,19 @@ package com.baeldung.web.log.app; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +import com.baeldung.web.log.config.CustomeRequestLoggingFilter; + import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @EnableAutoConfiguration @@ -14,4 +24,18 @@ public class Application extends SpringBootServletInitializer { public static void main(final String[] args) { SpringApplication.run(Application.class, args); } + + @Override + public void onStartup(ServletContext container) throws ServletException { + + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.setConfigLocation("com.baeldung.web.log"); + container.addListener(new ContextLoaderListener(context)); + + ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context)); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/"); + + container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher"); + } } \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java b/spring-rest/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java new file mode 100644 index 0000000000..3accb0a06e --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java @@ -0,0 +1,12 @@ +package com.baeldung.web.log.config; + +import org.springframework.web.filter.CommonsRequestLoggingFilter; + +public class CustomeRequestLoggingFilter extends CommonsRequestLoggingFilter { + + public CustomeRequestLoggingFilter() { + super.setIncludeQueryString(true); + super.setIncludePayload(true); + super.setMaxPayloadLength(10000); + } +} diff --git a/spring-rest/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java b/spring-rest/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java new file mode 100644 index 0000000000..1cc098abf1 --- /dev/null +++ b/spring-rest/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.web.controller; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.web.log.app.Application; +import com.baeldung.web.log.data.TaxiRide; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class TaxiFareControllerIntegrationTest { + + @LocalServerPort + private int port; + + @Test + public void givenRequest_whenFetchTaxiFareRateCard_thanOK() { + + String URL = "http://localhost:" + port + "/spring-rest"; + TestRestTemplate testRestTemplate = new TestRestTemplate(); + TaxiRide taxiRide = new TaxiRide(true, 10l); + String fare = testRestTemplate.postForObject( + URL + "/taxifare/calculate/", + taxiRide, String.class); + + assertThat(fare, equalTo("200")); + } +} \ No newline at end of file