title: How to add HTTP lifecycle middleware handler to spring?
body: <p>I have a <strong>CustomerController.java</strong>:</p>
  
  <pre><code>package com.satisfeet.http;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import com.satisfeet.core.model.Address;
  import com.satisfeet.core.model.Customer;
  import com.satisfeet.core.service.CustomerService;
  
  @RestController
  @RequestMapping("/customers")
  public class CustomerController {
  
      @Autowired
      private CustomerService service;
  
      @RequestMapping(method = RequestMethod.GET)
      public Iterable&lt;Customer&gt; index() {
          return this.service.list();
      }
  
      @RequestMapping(method = RequestMethod.POST)
      public Customer create(@RequestBody Customer customer) {
          this.service.create(customer);
  
          return customer;
      }
  
      @RequestMapping(method = RequestMethod.GET, value = "/{id}")
      public Customer show(@PathVariable Integer id) {
          return this.service.show(id);
      }
  
      @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
      public void update(@PathVariable Integer id, @RequestBody Customer customer) {
          this.service.update(id, customer);
      }
  
      @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
      public void destroy(@PathVariable Integer id) {
          this.service.delete(id);
      }
  
  }
  </code></pre>
  
  <p>and a <strong>ExceptionController.java</strong>:</p>
  
  <pre><code>package com.satisfeet.http;
  
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.bind.annotation.ResponseStatus;
  
  import com.satisfeet.core.exception.NotFoundException;
  
  @ControllerAdvice
  public class ExceptionController {
  
      @ExceptionHandler(NotFoundException.class)
      public ResponseEntity notFoundError() {
          return new ResponseEntity(HttpStatus.NOT_FOUND);
      }
  
  }
  </code></pre>
  
  <p>I would now like to add some sort of HTTP request-response middleware which is executed before the response is written and write the HTTP status code in json:</p>
  
  <pre><code>HTTP/1.1 404 OK
  Connection: close
  Content-Type: application/json
  
  {"error":"not found"}
  </code></pre>
  
  <p>I know how to convert a <code>HttpStatus</code> to a <code>String</code> but I do not know where I could do this globally with <code>@ControllerAdvice</code>.</p>
  
  <p><strong>So how do I register a global handler which has access to a response object?</strong></p>