whycxzp
2021-11-24 1afe70fac3969b6fec64c669ac7838e18e2f57f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.whyc.exception;
 
import com.whyc.dto.Response;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
import javax.servlet.http.HttpServletResponse;
 
/**
 * 针对RestController层捕捉异常,结果统一返回
 */
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class CustomExceptionResultHandler {
 
    /**错误捕捉,状态码:202*/
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public Response sendErrorResponse2Defined(Exception e, HttpServletResponse response){
        return new Response().setII(0,"接口请求异常,请联系软件人员进行处理.异常信息"+e.toString());
    }
 
 
}