title: Java create custom exception class with specific status code
body: <p>In Spring Boot, I create custom exception class with specific status code, and I call it to throw exception with code: 100 and message: "No have content" at controller, but output still returns "status": 500 and "error": "Internal Server Error"</p>
  
  <p><strong>AppException.java</strong></p>
  
  <pre><code>public class AppException extends RuntimeException {
  
      private static final long serialVersionUID = 1L;
  
      private final Integer code;
  
      public AppException(Integer code, String message) {
          super(message);
          this.code = code;
      }
  
      public Integer getCode() {
          return code;
      }
  }
  </code></pre>
  
  <p><strong>UserController.java</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/api/user")
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @GetMapping()
      public ApiResponseDto getAllUsers(Pageable pageable) {
          Page&lt;User&gt; users = userService.getAllUsers(pageable);
  
          if (users.getSize() &lt; 0) {
              throw new AppException(100, "No have content");
          }
  
          return new ApiResponseDto(HttpStatus.OK.value(), users);
      }
  </code></pre>
  
  <p><strong>Actual Output:</strong></p>
  
  <pre><code>{
      "timestamp": 1550987372934,
      "status": 500,
      "error": "Internal Server Error",
      "exception": "com.app.core.exception.AppException",
      "message": "No have content",
      "path": "/api/user"
  }
  </code></pre>
  
  <p><strong>My expectation:</strong></p>
  
  <pre><code>{
      "timestamp": 1550987372934,
      "status": 100,
      "error": "No have content",
      "exception": "com.app.core.exception.AppException",
      "message": "No have content",
      "path": "/api/user"
  }
  </code></pre>