GlobalExceptionHandler.java
1.38 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.glsx.exception;
import com.glsx.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 全局异常处理
* @author fengzhi
* @version V1.0
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 业务异常处理
*
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BusinessException.class)
public Result businessExceptionHandler(HttpServletRequest req, Exception e) {
log.error("业务异常:" + e.getMessage());
ApiException businessException = (ApiException) e;
return businessException.getResultEntity();
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List<String> errs = new ArrayList<>();
for (FieldError error : e.getBindingResult().getFieldErrors()) {
errs.add(error.getDefaultMessage());
}
return ApiErrorMessage.ARGS_NULL.exception(errs).getResultEntity();
}
}