How to control an exception when developing a RESTful API using java?

I am developing a RESTful API for our clients.
I am going to show error information if some kind of error occurs.
The error information log looks below.

{ "status": "failure", "error": { "message": "", "type": "", "code": 0000 } } 

At the programming level, how to handle exceptions?
Now I have created my own exception class that extends the Exception class. (not a RuntimeException)
Is this approach good or not? Is it better to use RuntimeExcepion?
My custom exception class ...

 public class APIException extends Exception { public enum Code { // duplicated exceptions ALREADY_REGISTERED(1001), // size exceptions OVER_KEYWORD_LIMIT(2001), OVER_CATEGORY_LIMIT(2002), TOO_SHORT_CONTENTS_LENGTH(2003), TOO_SHORT_TITLE_LENGTH(2004), // database exceptions DB_ERROR(3001), // unregistered exceptions UNREGISTERED_NAME(4001), // missing information exceptions MISSING_PARAMETER(5001), // invalid information exceptions INVALID_PARAMETER(6001), INVALID_URL_PATTERN(6002); private final Integer value; private Code(Integer value) { this.value = value; } public Integer getType() { return value; } } private final Code code; public APIException(Code code) { this.code = code; } public APIException(Code code, Throwable cause) { super(cause); this.code = code; } public APIException(Code code, String msg, Throwable cause) { super(msg, cause); this.code = code; } public APIException(Code code, String msg) { super(msg); this.code = code; } public Code getCode() { return code; } } 

And using an APIException class like this ...

 public void delete(int idx) throws APIException { try { Product product = productDao.findByIdx(idx); if (product.getCount() > 0) { throw new APIException(Code.ALREADY_REGISTERED, "Already registered product."); } productDao.delete(idx); } catch (Exception e) { throw new APIException(Code.DB_ERROR, "Cannot delete product. " + e.getMessage()); } } 

What is better to make a custom exception class or to use an existing exception such as unlargumentexception ..
If I can make my own exception class better, what should I distribute among the exceptions or the RuntimeException?
Please recommend me a good example, for example, my situation.
Thanks in advance.

+4
source share
1 answer

Since you are using Spring, I would recommend:

  • Extending RuntimeException and throwing exceptions to the controller

  • If your exception class models the attributes you want to return in the XML error, annotate the exception so that it can be returned as a response (including @ResponseStatus if they all have the same status code).

  • Deploy one or more @ExceptionHandler methods on your controller that returns an exception like @ResponseBody and ensures that the HttpServletResponse is correct. Sort of:

     @ExceptionHandler @ResponseBody public ErrorResponse handleAPIException(APIException e, HttpServletResponse response) { // Set any response attributes you need... return e; // or some other response } 
+3
source

All Articles