Skip to content

Commit f053451

Browse files
authored
Spring rest logging
Log incoming request
1 parent 75c9345 commit f053451

12 files changed

Lines changed: 328 additions & 0 deletions

File tree

‎spring-rest-logging/pom.xml‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-rest-logging</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<name>spring-rest-logging</name>
8+
<packaging>war</packaging>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.4.3.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- Spring Boot Dependencies -->
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
21+
</dependency>
22+
23+
<!-- Test -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-test</artifactId>
27+
</dependency>
28+
29+
<!-- Spring -->
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-web</artifactId>
33+
<exclusions>
34+
<exclusion>
35+
<artifactId>commons-logging</artifactId>
36+
<groupId>commons-logging</groupId>
37+
</exclusion>
38+
</exclusions>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-webmvc</artifactId>
43+
</dependency>
44+
45+
<!-- web -->
46+
47+
<dependency>
48+
<groupId>javax.servlet</groupId>
49+
<artifactId>javax.servlet-api</artifactId>
50+
<scope>provided</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-test</artifactId>
62+
</dependency>
63+
</dependencies>
64+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.rest.log.app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
import org.springframework.context.annotation.ComponentScan;
8+
9+
@EnableAutoConfiguration
10+
@ComponentScan("com.baeldung")
11+
@SpringBootApplication
12+
public class Application extends SpringBootServletInitializer {
13+
14+
public static void main(final String[] args) {
15+
SpringApplication.run(Application.class, args);
16+
}
17+
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.rest.log.app;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
10+
11+
@Component
12+
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
13+
14+
Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
15+
16+
@Override
17+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
18+
LOGGER.info("REQUEST URI: " + request.getRequestURI());
19+
return true;
20+
}
21+
22+
@Override
23+
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
24+
LOGGER.info("RESPONSE: " + response.getStatus());
25+
}
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.rest.log.config;
2+
3+
import javax.servlet.annotation.WebFilter;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.filter.CommonsRequestLoggingFilter;
8+
9+
@Configuration
10+
@WebFilter
11+
public class RequestLoggingFilterConfig {
12+
13+
@Bean
14+
public CommonsRequestLoggingFilter logFilter() {
15+
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
16+
filter.setIncludeQueryString(true);
17+
filter.setIncludePayload(true);
18+
filter.setMaxPayloadLength(10000);
19+
filter.setIncludeHeaders(false);
20+
filter.setAfterMessagePrefix("REQUEST DATA : ");
21+
return filter;
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.rest.log.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
import com.baeldung.rest.log.app.TaxiFareRequestInterceptor;
9+
10+
@Configuration
11+
public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter {
12+
13+
@Autowired
14+
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
15+
16+
@Override
17+
public void addInterceptors(InterceptorRegistry registry) {
18+
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.rest.log.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import com.baeldung.rest.log.data.RateCard;
15+
import com.baeldung.rest.log.data.TaxiRide;
16+
import com.baeldung.rest.log.service.TaxiFareCalculatorService;
17+
18+
@Controller
19+
public class TaxiFareController {
20+
21+
@Autowired
22+
private TaxiFareCalculatorService taxiFareCalculatorService;
23+
24+
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
25+
26+
@RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/")
27+
@ResponseBody
28+
public RateCard getTaxiFare() {
29+
LOGGER.debug("getTaxiFare() - START");
30+
return new RateCard();
31+
}
32+
33+
@RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/")
34+
@ResponseBody
35+
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
36+
LOGGER.debug("calculateTaxiFare() - START");
37+
return taxiFareCalculatorService.calculateFare(taxiRide);
38+
}
39+
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.rest.log.data;
2+
3+
public class RateCard {
4+
5+
private String nightSurcharge;
6+
private String ratePerMile;
7+
8+
public RateCard(){
9+
nightSurcharge="Extra $ 100";
10+
ratePerMile="$ 10 Per Mile";
11+
}
12+
13+
14+
public String getNightSurcharge() {
15+
return nightSurcharge;
16+
}
17+
public void setNightSurcharge(String nightSurcharge) {
18+
this.nightSurcharge = nightSurcharge;
19+
}
20+
public String getRatePerMile() {
21+
return ratePerMile;
22+
}
23+
public void setRatePerMile(String ratePerMile) {
24+
this.ratePerMile = ratePerMile;
25+
}
26+
27+
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.rest.log.data;
2+
3+
public class TaxiRide {
4+
5+
private Boolean isNightSurcharge;
6+
private Long distanceInMile;
7+
8+
public TaxiRide(){}
9+
10+
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){
11+
this.isNightSurcharge = isNightSurcharge;
12+
this.distanceInMile = distanceInMile;
13+
}
14+
15+
16+
public Boolean getIsNightSurcharge() {
17+
return isNightSurcharge;
18+
}
19+
20+
public void setIsNightSurcharge(Boolean isNightSurcharge) {
21+
this.isNightSurcharge = isNightSurcharge;
22+
}
23+
24+
public Long getDistanceInMile() {
25+
return distanceInMile;
26+
}
27+
28+
public void setDistanceInMile(Long distanceInMile) {
29+
this.distanceInMile = distanceInMile;
30+
}
31+
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.rest.log.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.baeldung.rest.log.data.TaxiRide;
6+
7+
@Service
8+
public class TaxiFareCalculatorService {
9+
10+
public String calculateFare(TaxiRide taxiRide) {
11+
Long fare = 0l;
12+
if (taxiRide.getIsNightSurcharge()) {
13+
fare = taxiRide.getDistanceInMile() * 10 + 100;
14+
} else {
15+
fare = taxiRide.getDistanceInMile() * 10;
16+
}
17+
return String.valueOf(fare);
18+
}
19+
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server.port= 9090
2+
server.context-path=/rest-log

0 commit comments

Comments
 (0)