Recommended style Java server project - a unified exception handling

1 Introduction

Consistent exception handling has two effects, the first is to help us will be extremely saved for subsequent processing, on the other hand, agreed to a deal as part of the response, rapid response to user processing results

2. Use

I use the SpringBoot frame, which comes with a method of exception handling, I just need to add your own exception handler on the line, at the same time, with a unified response to the previous chapter is exception handling more perfect.

@ControllerAdvice
 public  class ExceptionControllerAdvice {
     / ** 
     * capture parameters exception 
     * 
     * @param E exception request 
     * @param Request request information 
     * @param Response response information 
     * @return response abnormality information encapsulated
      * / 
    @ExceptionHandler ({an IllegalArgumentException. Class }) 
    @ResponseBody 
    public jsonResponse illegalArgumentExceptionHandler (exception E, the HttpServletRequest Request, the HttpServletResponse Response) { // , RedirectAttributes Attributes) {
         // TODO in remarkably
        = Message String e.getMessage ();
         IF (Message == null || message.length ()> 200 is ) { 
            Message = "Parameter error" ; 
        } 
        return Result.of (ResponseCodeConstant.ERROR_CODE_BUSINESS_EXCEPTION, Message); 
    } 
    / ** 
     other abnormal * 
     * 
     * @param E exception request 
     * @param request request information 
     * @param response response information 
     * @return response abnormality information encapsulated
      * / 
    @ExceptionHandler ({exception. class }) 
    @ResponseBody
    public Result exceptionHandler(Exception e, HttpServletRequest request, HttpServletResponse response) {
         //todo 保存异常
        return Result.of(ResponseStatus.INTERNAL_SERVER_ERROR);
    }
}

@ControllerAdvice is Spring comes annotation can be applied @InitBinder @ModelAttribute and @ExceptionHandler defined functions to all @RequeMapping in (@GetMapping so can be), the method @ InitBinder @ModelAttribute can explore on their own, this is mainly the use of @ ExceptionHandler do exception handling.

When @ExceptionHandler can pass an exception array, when @RequeMapping method throws an exception in the array, execute the exception handler to note is that you can define multiple exception handlers, throws an exception, from the to the next match, after the match to execute a function, no longer match down, so when writing exception-handling functions, a wide range of abnormal write the following, the last best add an exception exception handling can not match the exception

Author: suruns
link: http: //pipe.suruns.com/blogs/suruns/articles/2019/06/03/1559532092653
Source: Pipe
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/suruns/p/12174588.html