Spring Boot? 统一异常处理

Posted air_balloon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot? 统一异常处理相关的知识,希望对你有一定的参考价值。

效果区:

 

 

 代码区:

package com.wls.integrateplugs.exception.dto;


public class ErrorInfo<T> {

    public static final Integer OK = 200;
    public static final Integer ERROR = 500;

    private Integer code;
    private String message;
    private String url;
    private T data;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public static Integer getOK() {
        return OK;
    }

    public static Integer getERROR() {
        return ERROR;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

  

package com.wls.integrateplugs.exception;

import com.wls.integrateplugs.mvc.enums.RespEnum;

/**
 * Created by wls on 2017/8/7.
 */
public class MyException extends RuntimeException {
    private Integer code;

    public MyException() {
    }

    public MyException(RespEnum respEnum) {
        super(respEnum.getMsg());
        this.code = respEnum.getCode();
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}

  

package com.wls.integrateplugs.exception.controller;

import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.mvc.enums.RespEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/exception")
public class ExceptionController {
    @RequestMapping("/html")
    public String hello() throws Exception {
        throw new Exception("发生错误");
    }

    @RequestMapping("/json")
    public String json() throws MyException {
        throw new MyException(RespEnum.UNKNOW_CODE);
    }
}

  

package com.wls.integrateplugs.exception.handle;

import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.exception.dto.ErrorInfo;
import com.wls.integrateplugs.mvc.util.http.RespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wls on 2017/8/7.
 */
@ControllerAdvice
public class ExceptionHandle {

    Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    /*@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RespUtil handle(Exception e){
//        synchronized ()
        if(e instanceof MyException){
            MyException myException = (MyException) e;
            return RespUtil.error(myException.getCode(),myException.getMessage());
        }else{
            logger.info("【异常信息】:{}",e);
            return RespUtil.error(-1,"未知错误!");
        }
    }*/


    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("error");
        return mav;
    }

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(ErrorInfo.ERROR);
        r.setData("Some Data");
        r.setUrl(req.getRequestURL().toString());
        return r;
    }

}

  

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
    <meta charset="UTF-8" />
    <title>统一异常处理</title>
</head>
<body>
<h1>Error Handler</h1>
<div th:text="${url}"></div>
<div th:text="${exception.message}"></div>
</body>
</html>

  

以上是关于Spring Boot? 统一异常处理的主要内容,如果未能解决你的问题,请参考以下文章

知识点-Spring Boot 统一异常处理汇总

spring boot 2 统一异常处理

Spring Boot 统一异常处理

基于Spring Boot的统一异常处理设计

基于spring boot的统一异常处理

Spring Boot2 系列教程 (十四) | 统一异常处理