Step 1:
Create interceptor by
implementing HandlerInterceptor interface.
public class PerformanceInterceptor implements HandlerInterceptor { private long startTime; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { startTime = System.nanoTime(); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { long executionTime = System.nanoTime() - startTime; System.out.println( "Total execution time for " + request.getServletPath() + " is " + executionTime + " nano seconds"); } }
Step 2: Add the interceptor to specific path
patterns.
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new PerformanceInterceptor()).addPathPatterns("/api/v1/employees"); } }
Find the
below working application.
App.java
package com.sample.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
InterceptorConfig.java
package com.sample.app.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.sample.app.interceptors.PerformanceInterceptor; @Configuration public class InterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new PerformanceInterceptor()).addPathPatterns("/api/v1/employees"); } }
EmployeeController.java
package com.sample.app.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.sample.app.model.Employee; import com.sample.app.repository.EmployeeRepository; @RestController @RequestMapping("api/v1/") public class EmployeeController { @Autowired EmployeeRepository empRepo; @RequestMapping(value = "employees", method = RequestMethod.GET) public ResponseEntity<List<Employee>> all(){ return ResponseEntity.ok(empRepo.all()); } }
HomeController.java
package com.sample.app.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/") public String home() { return "Welcome to spring boot REST App development"; } }
PerformanceInterceptor.java
package com.sample.app.interceptors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.lang.Nullable; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class PerformanceInterceptor implements HandlerInterceptor { private long startTime; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { startTime = System.nanoTime(); return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { long executionTime = System.nanoTime() - startTime; System.out.println( "Total execution time for " + request.getServletPath() + " is " + executionTime + " nano seconds"); } }
Employee.java
package com.sample.app.model; public class Employee { private int id; private String firstName; private String lastName; private String country; public Employee(int id, String firstName, String lastName, String country) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.country = country; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
EmployeeRepository.java
package com.sample.app.repository; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Repository; import com.sample.app.model.Employee; @Repository public class EmployeeRepository { private static final List<Employee> emps = new ArrayList<>(); static { Employee emp1 = new Employee(1, "Sunil", "Dayanand", "India"); Employee emp2 = new Employee(2, "Keerthi", "Shetty", "India"); Employee emp3 = new Employee(3, "Ram", "Anand", "United States Of America"); emps.add(emp1); emps.add(emp2); emps.add(emp3); } public List<Employee> all() { return emps; } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springRest</groupId> <artifactId>springRest</artifactId> <version>1</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <name>springbootApp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Total
project structure looks like below.
Run
App.java.
Open the
url ‘http://localhost:8080/api/v1/employees’ in browser, you can see below kind
of message in console.
Total
execution time for /api/v1/employees is 138007646 nano seconds
You can
download complete working application from this link.
No comments:
Post a Comment