2
2
.
.
2
2
.
.
1
1
C
C
r
r
e
e
a
a
t
t
e
e
I
I
n
n
t
t
e
e
r
r
c
c
e
e
p
p
t
t
o
o
r
r
-
-
I
I
m
m
p
p
l
l
e
e
m
m
e
e
n
n
t
t
s
s
H
H
a
a
n
n
d
d
l
l
e
e
r
r
I
I
n
n
t
t
e
e
r
r
c
c
e
e
p
p
t
t
o
o
r
r
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to create Interceptor Class by using implements HandlerInterceptor.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_filter_create (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
Create Package: interceptors (inside main package)
Create Class: MyInterceptor.java (inside controllers package)
Create Class: InterceptorConfig.java (inside controllers package)
MyController.java
package com.ivoronline.springboot_interceptor.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
System.out.println("Controller:");
return "Hello from Controller";
}
}
MyInterceptor
MyController
Request
Response
http://localhost:8080/Hello
Request
Response
hello()
InterceptorConfig
MyInterceptor.java
package com.ivoronline.springboot_interceptor.interceptors;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor implements HandlerInterceptor {
//====================================================================
// PRE HANDLE
//====================================================================
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
System.out.print("MyInterceptor: preHandle() ");
System.out.println(request.getMethod());
return true;
}
//====================================================================
// POST HANDLE
//====================================================================
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) {
System.out.print("MyInterceptor: postHandle() ");
System.out.println(response.getStatus());
}
//====================================================================
// AFTER COMPLETION
//====================================================================
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception exception) {
System.out.print("MyInterceptor: afterCompletion() ");
System.out.println(response.getStatus());
}
}
InterceptorConfig.java
package com.ivoronline.springboot_interceptor.interceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Component
public class InterceptorConfig extends WebMvcConfigurationSupport {
@Autowired MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor);
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Console
MyInterceptor: preHandle() GET
Controller:
MyInterceptor: postHandle() 200
MyInterceptor: afterCompletion() 200
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>