Java Spring HTTP Request Interceptors

Dec 1st, 2020


Springboot logo

Springboot Web

Springboot provides a very powerful web/HTTP framework. From Rest web services, to static websites, Springboot is one of the fastest Java dev frameworks to work with.

Spring Request Scope

Thanks to Springboot's baked in request scope, we can define beans to keep track of data about an individual request.


@Component
@RequestScope
public class RequestDataBean {

  //constructor

  private UUID correlationId;

  //getters
  //setters 

}

The RequestDataBean tracks a correlation id, and can be injected into our Spring services and components.

Spring HTTP Request Interceptors

Spring HTTP request interceptors allow us to tap into a request at any stage in the request life cycle. Request interceptors allow you to do any pre/post processing your heart desires.

Logging Interceptor

Request interceptors open the doors to collecting all kinds of valuable request metrics that can provide priceless insight in regards to your application. One of the most common uses of request interceptors is logging.

To create an interceptor, you simply need to define a component that extends the Spring provided HandlerInterceptorAdapter class. The HandlerInterceptorAdapter provides us with several handy methods that we can override and tap into a request at various points in the request life cycle.


@Component
public class IncomingRequestLoggingInterceptor extends HandlerInterceptorAdapter {


  private static Logger logger = LoggerFactory.getLogger(IncomingRequestLoggingInterceptor.class);

  private RequestDataBean requestDataBean;

  @Autowired
  public IncomingRequestLoggingInterceptor(RequestDataBean requestDataBean) {
    this.requestDataBean = requestDataBean;
    this.requestDataBean.setCorrelationId(UUID.randomUuid());
  }

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

    logger.info("received http request", this.requestDataBean.getCorrelationId());
    return super.preHandle(request, response, handler);
  }

}

The logging interceptor captures the request when it comes in, assigns a correlation id, and produces an info log.

Response Latency

Another valuable metric we can capture is the latency between preHandle and afterCompletion.

Let's add a start time property to our request data bean


@Component
@RequestScope
public class RequestDataBean {

  //constructor

  private UUID correlationId;
  private Long startTime

  //getters
  //setters 

}

Now let's capture this information


@Component
public class IncomingRequestLoggingInterceptor extends HandlerInterceptorAdapter {


  private static Logger logger = LoggerFactory.getLogger(IncomingRequestLoggingInterceptor.class);

  private RequestDataBean requestDataBean;

  @Autowired
  public IncomingRequestLoggingInterceptor(RequestDataBean requestDataBean) {
    this.requestDataBean = requestDataBean;
    this.requestDataBean.setCorrelationId(UUID.randomUuid());
    this.requestDataBean.setStartTime(System.currentTimeMillis());
  }

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

    logger.info("received http request", this.requestDataBean.getCorrelationId());
    return super.preHandle(request, response, handler);
  }

}

Now let's create a response logging interceptor


@Component
public class ResponseLoggingInterceptor extends HandlerInterceptorAdapter {

  private RequestDataBean requestDataBean;

  //constructor

    @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    Long latency = System.currentTimeMillis() - 
    logger.info("proccessed request with latency", latency);
    super.afterCompletion(request, response, handler, ex);
  }

}

As you can see, the new interceptor will tell us how long the application takes to process requests.

Conclusion

HTTP request interceptors allow to capture a wide variety of valuable metrics about your application. We covered one of the most common application of request interceptors, the logging interceptor.

Comments




Navagation