title: Spring ResponseEntity return custom Status Code
body: <p>I'm trying to implement a custom HttpStatus code system to give errors informations on my REST services.</p>
  
  <p>To do so, i've implemented a new Enum, CustomHttpStatus</p>
  
  <pre><code>public enum CustomHttpStatus {
  
      ERROR1(42, "Error 1"),
      ERROR2(43, "Error 2"),
      ERROR3(44, "Error 3"),
      ERROR4(45, "Error 4"),
      ERROR5(46, "Error5");
  
      private CustomHttpStatus(int value, String reason) {
          this.value = value;
          this.reason = reason;
      }
  
      private final int value;
  
      private final String reason;
  
      public int getValue() {
          return value;
      }
  
      public String getReason() {
          return reason;
      }
  
  }
  </code></pre>
  
  <p>Then, i have a class which carries the information about the error and manages standard HTTPStatus and my CustomHttpStatus</p>
  
  <pre><code>public class ApiError {
  
      private HttpStatus status;
      private CustomHttpStatus customStatus;
      private String message;
      private List&lt;String&gt; errors;
  
      public HttpStatus getStatus() {
          return status;
      }
  
      public void setStatus(HttpStatus status) {
          this.status = status;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public List&lt;String&gt; getErrors() {
          return errors;
      }
  
      public void setErrors(List&lt;String&gt; errors) {
          this.errors = errors;
      }
  
  
  
      public ApiError(HttpStatus status, String message, List&lt;String&gt; errors) {
          super();
          this.status = status;
          this.message = message;
          this.errors = errors;
  
      }
  
      public ApiError(HttpStatus status, String message, String error) {
          super();
          this.status = status;
          this.message = message;
          errors = Arrays.asList(error);
  
      }
  
      public ApiError(CustomHttpStatus customStatus, String message, List&lt;String&gt; errors) {
          super();
          this.customStatus = customStatus;
          this.message = message;
          this.errors = errors;
  
      }
  
      public ApiError(CustomHttpStatus customStatus, String message, String error) {
          super();
          this.customStatus = customStatus;
          this.message = message;
          errors = Arrays.asList(error);
  
      }
  
  
  
  
      public CustomHttpStatus getCustomStatus() {
          return customStatus;
      }
  
      public void setCustomStatus(CustomHttpStatus customStatus) {
          this.customStatus = customStatus;
      }
  
  }
  </code></pre>
  
  <p>Next, in a <code>@ControllerAdvice</code> class i define how to respond to errors, for instance</p>
  
  <pre><code>    @ExceptionHandler(PlateNumberExistsExceptionAPI.class )
          protected ResponseEntity&lt;Object&gt; handlePlateNumberExistExceptionAPI(PlateNumberExistsExceptionAPI ex, WebRequest request) {
  
      StringBuilder builder = new StringBuilder();
     builder.append(ex.getMessage());
      ApiError apiError = new ApiError(CustomHttpStatus.ERROR1, 
      ex.getLocalizedMessage(), builder.substring(0,      builder.length()));
      logger.debug("ApiError is " + apiError.toString());
      logger.error("Plate exists - API", ex);
      return ResponseEntity.status(apiError.getCustomStatus().getValue())
  .headers(new HttpHeaders())    
  .contentType(MediaType.APPLICATION_JSON).body(apiError);
  
  
           }
  </code></pre>
  
  <p>The problem is that i need to use a <code>BodyBuilder</code> for ResponseEntity which constructor want a <code>HttpStatus</code> value. The application responds with my custom error, but with no message and it takes 60000 ms to answer...
  I don't know why it takes so long, the exception is soon hit and the ApiError generated, the problem is on the response</p>
  
  <p><a href="https://i.stack.imgur.com/5l4j8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5l4j8.png" alt="PostMan Test"></a></p>