title: Spring Boot RestController: Intercept incoming requests
body: <p>I am currently writing some kind of Framework that would allow others to write REST Controllers for it. Naturally, I want those "others" to have as little interaction with whats happening in my code as possible.</p>
  
  <p>Specifically, I want and need to access the requests data (i.e. 
  <code>RequestEntity</code> before the request is handled by the rest controller. Sort of "intercepting" a request before it is handled by the controller, and only then letting the controller handle it.</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>@RestController
  @RequestMapping("/")
  public class MyController {
  
      @GetMapping("/")
      @ResponseBody
      public ResponseEntity&lt;String&gt; getSomething(RequestEntity requestEntity) {
  
          MyClass.doStuffWithRequestEntity(requestEntity);
          // ...
  </code></pre>
  
  <p>Now what I would need is that <code>ExternalClass.doStuffWithRequestEntity(requestEntity);</code> is called without the need to explicitly call it. Is it possible to have some method in some class being called (with the RequestEntity passed to it!) without having to call it explicitly?</p>
  
  <p>Furthermore, said Interceptor class should create and configure an object that is then again made available to the rest controller.</p>
  
  <p>I'd be thinking something like</p>
  
  <pre><code>class RestController {
      @RestController
      @RequestMapping("/")
      public class MyController {
  
          @GetMapping("/")
          @ResponseBody
          public ResponseEntity&lt;String&gt; getSomething() {
  
              MyClass x = MyClass.getInstanceCreatedByInterceptor();
          }
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Interceptor {
      public void doStuffWithRequestEntity(requestEntity) {
  
          MyClass x = new MyClass();
          x.fillObjectWithData();
      }
  }
  </code></pre>
  
  <p>being executed before.</p>
  
  <p>The idea is that EVERY (!) incoming request is parsed and its contents get decoded without the programmer of the rest controller getting having to care about this at all. They should just access data via/from the MyClass instance.</p>
  
  <p>Is there a way to do this?</p>