Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring Boot: Adding interceptor to spring boot application

Spring Boot: Adding Interceptor To Spring Boot Application
This is continuation to my previous post. You can download previous working application from this link.

Step 1: Create interceptor class.
PerformanceReportInterceptor.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 PerformanceReportInterceptor 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;
modelAndView.addObject("executionTime", executionTime);
}
}



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring Boot: Adding interceptor to spring boot application

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×