title: Spring controller throwing HttpStatus.UNAUTHORIZED fires 500 Http error instead of 401
body: <p>Here's the scenario :
  I created the following custom response exception, to fire the 401 Http Status :</p>
  
  <pre><code>@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
  public class HttpUnauthorizedException extends RuntimeException {
  
  }
  </code></pre>
  
  <p>The controller that uses the exception :</p>
  
  <pre><code>@Controller
  public UserController {
      @RequestMapping(value = "api/user")
      @ResponseBody
  
      public String doLogin(
                   @RequestParam(value = "username", required = false) String username, @RequestParam(value = "password", required = false) String password) {
          if(userLoggedIn(String username, String password)) {
               return "OK";
          }
          else {
               throw new HttpUnauthorizedException();
          }
      }
     ...
  }
  </code></pre>
  
  <p>Now when I try to access the controller to see the 401 exception, the server fires the Http error code 500 instead. But interestingly enough, when I try with the <code>HttpStatus.NOT_FOUND</code> it actually works, the server fires 404. Is there something I'm missing on here?</p>
  
  <p>Thanks in advance :-)</p>