springboot2-request-logger - implemented spring handler interceptor
parent
1016e38700
commit
44ea2370a2
|
@ -62,3 +62,54 @@ You may run [testControllerLoggingWithFilter](./src/test/java/com/bvn13/example/
|
|||
|
||||
So our goal is achieved. We can see all files we request and not existing file too.
|
||||
|
||||
|
||||
## Building a Handler Interceptor
|
||||
|
||||
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.
|
||||
|
||||
You can check out Spring class `org.springframework.web.servlet.handler.HandlerInterceptorAdapter` to see its methods we are able to implement in our handler.
|
||||
|
||||
Lets [our handler](/src/main/java/com/bvn13/example/springboot/springrequestlogger/handlers/RequestLoggingHandler.java) implement `preHandle` and `postHandle` events:
|
||||
|
||||
```java
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
log.debug(
|
||||
String.format("HANDLER(pre) URL: %s", request.getRequestURI())
|
||||
);
|
||||
|
||||
return super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
log.debug(
|
||||
String.format("HANDLER(post) URL: %s", request.getRequestURI())
|
||||
);
|
||||
|
||||
super.postHandle(request, response, handler, modelAndView);
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class WebApplicationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Setter(onMethod_ = @Autowired)
|
||||
private RequestLoggingHandler requestLogger;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(requestLogger);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Result
|
||||
|
||||
Lets start our test to check out the result!
|
||||
|
||||
![](./img/2019-09-30_23-25.png)
|
||||
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 307 KiB |
|
@ -0,0 +1,36 @@
|
|||
package com.bvn13.example.springboot.springrequestlogger.handlers;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author bvn13
|
||||
* @since 30.09.2019
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RequestLoggingHandler extends HandlerInterceptorAdapter {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
log.debug(
|
||||
String.format("HANDLER(pre) URL: %s", request.getRequestURI())
|
||||
);
|
||||
|
||||
return super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
log.debug(
|
||||
String.format("HANDLER(post) URL: %s", request.getRequestURI())
|
||||
);
|
||||
|
||||
super.postHandle(request, response, handler, modelAndView);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.bvn13.example.springboot.springrequestlogger.handlers;
|
||||
|
||||
import lombok.Setter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author bvn13
|
||||
* @since 30.09.2019
|
||||
*/
|
||||
@Configuration
|
||||
public class WebApplicationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Setter(onMethod_ = @Autowired)
|
||||
private RequestLoggingHandler requestLogger;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(requestLogger);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue