All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.starmcc.qmframework.exception.QmExceptionHandler Maven / Gradle / Ivy

Go to download

springboot based on a series of packaging framework. QmFramework to implements AOP, rewrite the requestBody, global exception catching,version checking, request body parameter symmetric encryption mechanism, controller packaging and the toolkit collection. thank you for using it.

There is a newer version: 3.0.1-RELEASE
Show newest version
package com.starmcc.qmframework.exception;


import com.starmcc.qmframework.controller.QmCode;
import com.starmcc.qmframework.controller.QmController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 全局异常处理、将程序中可能出现的异常进行捕获,返回JSON格式的错误信息。
 *
 * @Author qm
 * @Date 2018年11月24日 上午1:30:20
 */
@ControllerAdvice
@Controller
@RequestMapping(value = "/error")
public class QmExceptionHandler extends QmController {

    private static final Logger LOG = LoggerFactory.getLogger(QmExceptionHandler.class);

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public String httpRequestMethodNotSupportedException(HttpServletResponse response,
                                                         Exception e) {
        LOG.info("请求方式错误,请核实请求方式 `GET` and `POST`");
        response.setStatus(200);
        return super.sendJSON(QmCode._405);
    }

    @ExceptionHandler(QmParamNullException.class)
    @ResponseBody
    public String qmParamNullException(HttpServletResponse response,
                                       Exception e) {
        LOG.info("缺少某些请求参数,请核实请求参数是否正确!");
        e.printStackTrace();
        response.setStatus(200);
        return super.sendJSON(QmCode._100);
    }

    @ExceptionHandler(QmParamErrorException.class)
    @ResponseBody
    public String qmParamErrorException(HttpServletResponse response,
                                        Exception e) {
        LOG.info("请求参数错误,请核实请求参数是否正确!");
        response.setStatus(200);
        return super.sendJSON(QmCode._101);
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public String httpMediaTypeNotSupportedException(HttpServletResponse response,
                                                     Exception e) {
        response.setStatus(200);
        return super.sendJSON(QmCode._415);
    }


    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public String notFoundPage404(HttpServletResponse response,
                                  Exception e) throws IOException {
        LOG.info("请求地址错误,请核实请求地址是否正确!");
        response.setStatus(200);
        return super.sendJSON(QmCode._404);
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String defaultException(HttpServletResponse response, Exception e) throws IOException {
        LOG.info("服务器遇到了错误,请检查相关问题!");
        e.printStackTrace();
        response.setStatus(200);
        return super.sendJSON(QmCode._500);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy