青羊经侦大队-数据平台
安瑾然
2022-07-12 da52a1ba7e8fc2d8e633655484e7ff4abed319c8
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
package com.example.jz.config;
 
import com.example.jz.enums.BusinessHttpStatus;
import com.example.jz.exception.BusinessException;
import com.example.jz.modle.entity.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
/**
 * 自定义错误处理器
 */
@Controller
@RestControllerAdvice
public class DefaultExceptionHandlerConfig {
 
 
    @ExceptionHandler(BindException.class)
    public R<String> bindExceptionHandler(BindException e) {
        e.printStackTrace();
        return R.failed(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
 
    }
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        e.printStackTrace();
        return R.failed(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
    }
 
    @ExceptionHandler(BusinessException.class)
    public R<String> unauthorizedExceptionHandler(BusinessException e) {
        e.printStackTrace();
        return R.failed(e.getMessage());
    }
}