diff --git a/spring-rest-logging/pom.xml b/spring-rest-logging/pom.xml index 5924650e5f..799a746b63 100644 --- a/spring-rest-logging/pom.xml +++ b/spring-rest-logging/pom.xml @@ -12,7 +12,7 @@ spring-boot-starter-parent 1.4.3.RELEASE - + @@ -60,5 +60,14 @@ org.springframework spring-test + + + + commons-io + commons-io + 2.4 + + + diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java index bc011a4db7..4c187772cc 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java @@ -7,6 +7,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; +import org.springframework.web.util.ContentCachingRequestWrapper; + +import com.baeldung.rest.log.util.RequestLoggingUtil; @Component public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { @@ -15,7 +18,21 @@ public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - LOGGER.info("REQUEST URI: " + request.getRequestURI()); + String postData = null; + HttpServletRequest requestCacheWrapperObject = null; + try { + // Uncomment to produce the stream closed issue + // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream()); + + // To overcome request stream closed issue + requestCacheWrapperObject = new ContentCachingRequestWrapper(request); + requestCacheWrapperObject.getParameterMap(); + } catch (Exception exception) { + exception.printStackTrace(); + } finally { + postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject); + LOGGER.info("REQUEST DATA: " + postData); + } return true; } diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java index 158ba51f77..7afb34dd11 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java @@ -1,13 +1,10 @@ package com.baeldung.rest.log.config; -import javax.servlet.annotation.WebFilter; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CommonsRequestLoggingFilter; @Configuration -@WebFilter public class RequestLoggingFilterConfig { @Bean diff --git a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java index 0d8e4dafa4..1ef9ece0b2 100644 --- a/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java +++ b/spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java @@ -34,7 +34,10 @@ public class TaxiFareController { @ResponseBody public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { LOGGER.debug("calculateTaxiFare() - START"); - return taxiFareCalculatorService.calculateFare(taxiRide); + String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); + LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare); + LOGGER.debug("calculateTaxiFare() - END"); + return totalFare; } }