WHAT IS INTERCEPTOR?
Interceptor is work as a middle object. Interceptor handles the HTTP request before it executes by the controller's method. If you want to perform some task before the controller start executes the HTTP request then we use to the handler adapter and use by dispatcher servlet. Handler Adapter uses handler interceptor to perform the action. Handler interceptor has various methods which are used to perform the action before handling, after handling and after completion. Using interceptor we can perform the operation before request handled by the controller and after the controller send the request to the client.
WHAT IS SPRING BOOT INTERCEPTOR?
In spring boot to implement interceptor, we must implement the Handler Interceptor interface. Handler Interceptor interface intercepts the client request before the request process in the controller. Handler Interceptor interface has 3 methods which are,
The preHandle method is used to make operations before the client request handled by the controller. preHandle method return boolean value i.e. true or false. If the preHandle method returns true then spring know that the interceptor provides the request to the next process to another interceptor is exist or to the controller to execute the request. If interceptor sends false value then spring service knows that no more execution needs to the request, all process handled by the interceptor.
The postHandle method is used to perform operations after the controller executes the request completely but not render the result in view. This method is used to update or add an additional attribute to the view page.
1.boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler):
The preHandle method is used to make operations before the client request handled by the controller. preHandle method return boolean value i.e. true or false. If the preHandle method returns true then spring know that the interceptor provides the request to the next process to another interceptor is exist or to the controller to execute the request. If interceptor sends false value then spring service knows that no more execution needs to the request, all process handled by the interceptor.
2.void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView):
The postHandle method is used to perform operations after the controller executes the request completely but not render the result in view. This method is used to update or add an additional attribute to the view page.
3.void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex):
This method is called after complete all the task. When the controller executes the request completely and the result renders in view pages this method was called.
4.void afterConcurrentHandlingStarted(HttpServletRequest request,HttpServletResponse response,Object handler):
This method used to handle the asynchronous request. This method callback after asynchronous request handling.
When handler starts to execute an async request, the DispatcherServlet exit without calling the postHandle and afterCompletion method which is for synchronous request. Implementations may use the provided request and response but should avoid modifying them in ways that would conflict with the concurrent execution of the handler. A typical use of this method would be to clean up thread-local variables.
MAVEN DEPENDENCY:
PROJECT STRUCTURE:
DeveloperVillagerApplication.java
package com.dv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = { "com.dv"})
public class DevelpoerVillagerApplication {
public static void main(String[] args) {
SpringApplication.run(DevelpoerVillagerApplication.class, args);
}
}
ServletInitializer.java
package com.dv;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DevelpoerVillagerApplication.class);
}
}
WebMvcConfig.java
package com.dv;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DVInterceptorConfig());
}
}
DVInterceptorConfig.java
package com.dv;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class DVInterceptorConfig extends HandlerInterceptorAdapter {
private static Logger logger = LoggerFactory.getLogger(DVInterceptorConfig.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.info("In DVInterceptorConfig calss preHandler method. with the execution start time:{}",new Date(System.currentTimeMillis()));
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
logger.info("In DVInterceptorConfig calss postHandle method. with the execution start time:{}",new Date(System.currentTimeMillis()));
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("In DVInterceptorConfig calss afterCompletion method. with the execution start time:{}",new Date(System.currentTimeMillis()));
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.info("In DVInterceptorConfig calss preHandler method. with the eecution start time:{}",new Date(System.currentTimeMillis()));
}
}
DVController.java
package com.dv;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DVController {
private static Logger logger = LoggerFactory.getLogger(DVController.class);
@RequestMapping(value = "test", method = RequestMethod.GET)
public void executeTestMethod() {
logger.info("In DVController calss executeTestMethod method. with the execution start time:{}",new Date(System.currentTimeMillis()));
}
}
Very good post
ReplyDelete