RESTFul 全局异常处理
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(ServiceException.class)
@ResponseBody
public ErrorResponse<String> serviceException(HttpServletResponse response, ServiceException e) {
//设置响应状态码
response.setStatus(e.getCode().getCode());
return ErrorResponse.ofException(e.getCode(), e.getMessage());
}
@ExceptionHandler
//设置响应状态码
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ErrorResponse<String> allException(Exception e) {
e.printStackTrace();
return new ErrorResponse<String>(HttpStatus.INTERNAL_SERVER_ERROR.value(),e.getMessage());
}
}