springmvc全局异常统一处理

2018年03月07日 14:54 | 2618次浏览 作者原创 版权保护

一、前沿

       一般项目中都需要作异常处理,基于系统架构的设计考虑,使用统一的异常处理方法。包括预期可能发生的异常、运行时异常(RuntimeException),运行时异常不是预期会发生的。针对预期可能发生的异常,在代码手动处理异常可以try/catch捕获,可以向上抛出。针对运行时异常,只能通过规范代码质量、在系统测试时详细测试等排除运行时异常。

二、统一异常处理

2.1定义异常

针对预期发生的异常,定义很多异常类型,这些异常继承Exception。

package com.ycy.Exception;

/**
 *  系统自定义系统异常,实际开发时需要多个
 * 
 */
public class CustomException extends  Exception{
    //异常信息
    private  String  message;

    public CustomException(String message){
        super(message);
        this.message=message;
    }

    public String getMessage() {
        return message;
    }

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

2.2 异常处理

要在一个统一异常处理的类中要处理系统抛出的所有异常,根据异常类型来处理。

2.2.1异常JAVA处理

package com.ycy.Exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {
    //handler :最终需要执行的handler,就是handlerMethod
    //ex:异常信息
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
                                         Object handler, Exception ex) {
        //输出异常
        ex.printStackTrace();
         //统一异常处理
        String message=null;
        CustomException customException=null;
        if(ex instanceof  CustomException){
            //统一异常: 测试需全部测出
            customException=(CustomException)ex;
            message=customException.getMessage();
        }else{
            //未知异常: 测试需全部测出
             customException=new  CustomException("恭喜你,你可以领奖,你发现这个漏洞,联系客服:400");
        }
        request.setAttribute("message",message);
        try {
            //错误页面
            request.getRequestDispatcher("/pages/jsp/error.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ModelAndView();
    }
}

2.2.2 异常页面展示

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title></title>
</head>
<body>
${message}
</body>
</html>

2.3 异常测试

    public ItemsCustom getItemsById(Integer id) throws  Exception{
        Items items = itemsMapper.getItemsById(id);
        //在这里随着需求的变量,需要查询商品的其它的相关信息,返回到controller
        if(items==null){
            throw new CustomException("修改商品信息不存在");
        }
        ItemsCustom itemsCustom = new ItemsCustom();
        //将items的属性拷贝到itemsCustom
        BeanUtils.copyProperties(items, itemsCustom);

        return itemsCustom;

    }

测试访问:http://localhost:8080/spring01/items/editItems?id=99

spring教程

2.4 异常小结

首先我们只能捕获我们定义的异常,否则其他异常为没有测试好,没有发现异常,或者程序错误。注意:要求我们controller、service、dao都向上抛出异常。

spring异常处理



小说《我是全球混乱的源头》
此文章本站原创,地址 https://www.vxzsk.com/612.html   转载请注明出处!谢谢!

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程


上一篇:体系结构 下一篇:环境设置
^