涓��变袱涓���浠�,涓�涓�澶����ㄥ�寮�甯�,淇�海洋�淇℃���版�ュ�,��澶�涓�涓�璐�璐h���寮�甯镐俊��缁��ュ��,��瑕�广大��舵��浠舵坊���伴」��涓�,���������朵���缃��冲��

1. MyExceptionHandler.java �ㄥ�寮�甯稿���绫�

浼����ц�姝ょ把,杩���涓�濂芥����404��403绛���璇�淇℃��
getMaps()璇峰�����山��request涓�浼���杩��ョ�����颁俊��
getHeaders()璇峰�����山��request涓�浼���杩��ョ��header淇℃��
浠g��濡�涓�锛�

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * <h5>��杩�:�ㄥ�寮�甯稿���绫�</h5>
 * 浼����ц�姝ゅ�甯稿���绫�
 *  
 */
@ControllerAdvice
public class MyExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(value =Exception.class)
    public String exceptionHandler(HttpServletRequest request, Exception e){
        Map<String, Object> paramsMap = getMaps(request);
        Map<String, Object> headersMap = getHeaders(request);

        String requestUri = request.getAttribute("org.springframework.web.servlet.HandlerMapping.lookupPath").toString();
        log.error("璇锋�[{}]����[{}]寮�甯�\r\n����[{}]\r\nheader[{}]", requestUri,e.getMessage(),paramsMap,headersMap, e);

        // 杩�����璇�淇℃��,河渠ょ��朵�寮�甯稿���绫诲���
        return e.getMessage();
    }

    // =================== private method ===================

    /**
     * <h5>����:�山��浠�request涓�浼���杩��ョ�����颁俊��</h5>
     * 
     * @return Map<String, Object>
     */
    private Map<String, Object> getMaps(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Enumeration<String> enume = request.getParameterNames();
        while (enume.hasMoreElements()) {
            String key = (String) enume.nextElement();
            String[] values = request.getParameterValues(key);
            paramMap.put(key, values.length == 1 ? request.getParameter(key).trim() : values);
        }

        return paramMap;
    }

    /**
     * <h5>����: �山��浠�request涓�浼���杩��ョ��header淇℃��</h5>
     * 
     * @return Map<String, Object>
     */
    private Map<String, Object> getHeaders(HttpServletRequest request) {
        Map<String, Object> headerMap = new HashMap<String, Object>();
        Enumeration<?> er = request.getHeaderNames();//�山��璇锋�澶寸������name��
        String headerName;
        while(er.hasMoreElements()){
            headerName = er.nextElement().toString();
            headerMap.put(headerName, request.getHeader(headerName));
        }

        return headerMap;
    }
}

2. MyErrorController.java �ㄥ���璇�淇℃��澶���绫�

���ц�姝ょ把,姝ょ把��浠ユ��������,404��403绛�淇℃��锛�浠g��濡�涓�锛�

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.jnxj.common.vo.MessageBean;

/**
 * <h5>��杩�:�ㄥ���璇�淇℃��澶���</h5>
 *  �ц�疏浚�寮�甯稿���绫讳����ц�姝ょ把
 */
@RestController
public class MyErrorController implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        String message;
        // �山��statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == 400) {
            message = "璇�涔���璇�,褰���璇锋���娉�琚����″�ㄧ��瑙f��璇锋����版��璇�";
        } else if (statusCode == 401) {
            message = "褰���璇锋���瑕��ㄦ�烽��璇�";
        } else if (statusCode == 403) {
            message = "����涓�瓒�";
        } else if (statusCode == 404) {
            message = "璇锋���璧�婧�涓�海洋���";
        } else {
            message = "绯荤�涓�广大�蹇�����河渠�,姝e�ㄧ揣�ヤ慨澶�涓�,璇疯��蹇�绛���...o(�ワ���)o";
        }

        MessageBean messageBean = new MessageBean();
        messageBean.setCode(statusCode.toString());
        messageBean.setMessage(message);
        return JSONObject.toJSONString(messageBean);
    }
}