title: How to return http status code for exceptions in rest services
body: <p>In my application I have different layers like the rest layer, service layer and DB layer, according to business scenarios I am trowing different business exceptions from the service layer.</p>
  
  <p>But now, I have to set different HTTP codes like 400, 403, 409, 412.. to REST responses.</p>
  
  <p>How can I set different HTTP status codes based on different scenarios?</p>
  
  <p>Which is the most feasible way like: aspect, exception mapper, or ....?</p>
  
  <p>Since I can set HTTP status only once in rest layer (
  <a href="https://stackoverflow.com/questions/4687271/jax-rs-how-to-return-json-and-http-status-code-together">referred this</a> ), I am not able to map to different HTTP codes because my exception is from service layer.</p>
  
  <p>My exception class looks like this:</p>
  
  <pre><code>public class BusinessException extends RuntimeException {
      private static final long serialVersionUID = 1L;
  
      public BusinessException(ErrorEnumeration error) {
  
      }
      public BusinessException(Exception e, ErrorEnumeration error) {
  
      }
  }
  </code></pre>
  
  <p>and the exception will be thrown from the service like this:</p>
  
  <pre><code> throw new BusinessException(ErrorEnumeration.VALIDATION_FAILED);
  </code></pre>
  
  <p>Please help by suggesting a solution</p>