@ControllerAdvice

package com.medimpact.pbm.pbmbddsvc.exception;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.medimpact.pbm.pbmbddsvc.domain.BddResponse;
import com.medimpact.pbm.pbmbddsvc.domain.ResponseConstant;

@ControllerAdvice
public class GlobalExceptionHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
    
    
    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public BddResponse baseException(BaseException e){
        BddResponse resp = new BddResponse();
        resp.setRespCode(e.getCode());
        resp.setRespDesc(e.getDesc()+":"+e.getDetail());
        resp.setRespValue(e.getMessage());
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    }
      // <2> 处理 json 请求体调用接口校验失败抛出的异常
    //ResultInfo<List<String>>
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BddResponse methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> collect = fieldErrors.stream()
                .map(o -> o.getDefaultMessage())
                .collect(Collectors.toList());
        BddResponse resp = new BddResponse();
        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
        resp.setRespValue(collect);
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    }

 // <4> 处理Header传入参数抛出的异常
    @ExceptionHandler(MissingRequestHeaderException.class)
    public BddResponse missingRequestHeaderExceptionHandler(MissingRequestHeaderException e) {
        String message = e.getMessage();
        //return new ResultInfo<String>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, message);
        BddResponse resp = new BddResponse();
        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
        resp.setRespValue(message);
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    
    }
    // <5> 处理传入参数抛出的异常
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public BddResponse httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        String message = e.getMessage();
        //return new ResultInfo<String>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, message);
        BddResponse resp = new BddResponse();
        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
        resp.setRespValue(message);
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    }
    // <6>
    @ExceptionHandler(ServletException.class)
    public BddResponse servletExceptionHandler(ServletException e) {
        String message = e.getMessage();
        //return new ResultInfo<String>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, message);
        BddResponse resp = new BddResponse();
        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
        resp.setRespValue(message);
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    }
    
    
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BddResponse unknownException(Exception e){
         BddResponse resp = new BddResponse();
         resp.setRespCode(ResponseConstant.UNKNOWN_EXCEPTION.getRespCode());
         resp.setRespDesc(ResponseConstant.UNKNOWN_EXCEPTION.getRespDesc());
         resp.setRespValue(e.getMessage());
         logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
         return resp;
    }
//    // <1> 处理 form data方式调用接口校验失败抛出的异常
//    @ExceptionHandler(BindException.class)
//    public BddResponse bindExceptionHandler(BindException e) {
//        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
//        List<String> collect = fieldErrors.stream()
//                .map(o -> o.getDefaultMessage())
//                .collect(Collectors.toList());
//        //return new ResultInfo<List<String>>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect);
//        BddResponse resp = new BddResponse();
//        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
//        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
//        resp.setRespValue(collect);
//        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
//        return resp;
//    
//    }
 
    
    
//  @ExceptionHandler(IOException.class)
//  public BddResponse ioExceptionHandler(IOException e) {
//      String message = e.getMessage();
//      //return new ResultInfo<String>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, message);
//      BddResponse resp = new BddResponse();
//      resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
//      resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
//      resp.setRespValue(message);
//      logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
//      return resp;
//  }
//    
    // <3> 处理单个参数校验失败抛出的异常
    @ExceptionHandler(ConstraintViolationException.class)
    public BddResponse constraintViolationExceptionHandler(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        List<String> collect = constraintViolations.stream()
                .map(o -> o.getMessage())
                .collect(Collectors.toList());
        //return new ResultInfo<List<String>>(HttpStatus.BAD_REQUEST.value(), BAD_REQUEST_MSG, collect);
        BddResponse resp = new BddResponse();
        resp.setRespCode(""+ResponseConstant.PARAM_INVALID.getRespCode());
        resp.setRespDesc(ResponseConstant.PARAM_INVALID.getRespDesc());
        resp.setRespValue(collect);
        logger.error(resp.getRespCode()+"-"+resp.getRespDesc(),e);
        return resp;
    }
    
}

原文地址:https://www.cnblogs.com/zzsuje/p/14048895.html