Skip to content

Commit 44ea237

Browse files
committed
springboot2-request-logger - implemented spring handler interceptor
1 parent 1016e38 commit 44ea237

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

springboot2-request-logger/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,54 @@ You may run [testControllerLoggingWithFilter](./src/test/java/com/bvn13/example/
6262

6363
So our goal is achieved. We can see all files we request and not existing file too.
6464

65+
66+
## Building a Handler Interceptor
67+
68+
Handler interceptors have more power to process your requests since they handle not the fact of request but its events: before request, after request, after completion.
69+
70+
You can check out Spring class `org.springframework.web.servlet.handler.HandlerInterceptorAdapter` to see its methods we are able to implement in our handler.
71+
72+
Lets [our handler](/src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/RequestLoggingHandler.java) implement `preHandle` and `postHandle` events:
73+
74+
```java
75+
@Override
76+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
77+
log.debug(
78+
String.format("HANDLER(pre) URL: %s", request.getRequestURI())
79+
);
80+
81+
return super.preHandle(request, response, handler);
82+
}
83+
84+
@Override
85+
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
86+
log.debug(
87+
String.format("HANDLER(post) URL: %s", request.getRequestURI())
88+
);
89+
90+
super.postHandle(request, response, handler, modelAndView);
91+
}
92+
```
93+
94+
But the handler will not work until we actually tell Spring to use it. We must build a [configuration](/src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/WebApplicationConfiguration.java) to enable our handler.
95+
96+
```java
97+
@Configuration
98+
public class WebApplicationConfiguration implements WebMvcConfigurer {
99+
100+
@Setter(onMethod_ = @Autowired)
101+
private RequestLoggingHandler requestLogger;
102+
103+
@Override
104+
public void addInterceptors(InterceptorRegistry registry) {
105+
registry.addInterceptor(requestLogger);
106+
}
107+
}
108+
```
109+
110+
### Result
111+
112+
Lets start our test to check out the result!
113+
114+
![](./img/2019-09-30_23-25.png)
115+
307 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bvn13.example.springboot.springrequestlogger.handlers;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.servlet.ModelAndView;
6+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
* @author bvn13
13+
* @since 30.09.2019
14+
*/
15+
@Slf4j
16+
@Component
17+
public class RequestLoggingHandler extends HandlerInterceptorAdapter {
18+
19+
@Override
20+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
21+
log.debug(
22+
String.format("HANDLER(pre) URL: %s", request.getRequestURI())
23+
);
24+
25+
return super.preHandle(request, response, handler);
26+
}
27+
28+
@Override
29+
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
30+
log.debug(
31+
String.format("HANDLER(post) URL: %s", request.getRequestURI())
32+
);
33+
34+
super.postHandle(request, response, handler, modelAndView);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bvn13.example.springboot.springrequestlogger.handlers;
2+
3+
import lombok.Setter;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
9+
/**
10+
* @author bvn13
11+
* @since 30.09.2019
12+
*/
13+
@Configuration
14+
public class WebApplicationConfiguration implements WebMvcConfigurer {
15+
16+
@Setter(onMethod_ = @Autowired)
17+
private RequestLoggingHandler requestLogger;
18+
19+
@Override
20+
public void addInterceptors(InterceptorRegistry registry) {
21+
registry.addInterceptor(requestLogger);
22+
}
23+
}

0 commit comments

Comments
 (0)