• javaweb+spring 项目集成异常的处理


    在web项目开发中,一个系统应该要考虑到异常情况的处理,并且应该当异常发生时应该需要记录相应的异常日志,对于用户而言则不能直接抛出异常,需要考虑到用户的体验:

    以下就介绍基于spring框架的基础上的异常处理:

    1:如何将异常优雅地显示给用户?

    如果将一连串的java报错展示给用户,对用户而言是没有意义的,并且也是危险的;

    1)定义错误代码:这里以一个枚举类来封装用户错误代码:ErrorCode:

    public enum ErrorCode {
    
        NULL_OBJ("LUO001","对象为空"),
        ERROR_ADD_USER("LUO002","添加用户失败"),
        UNKNOWN_ERROR("LUO999","系统繁忙,请稍后再试...."),
        FILE_NOT_FOUND("FILE001","你要读取的资源找不到");
        
        private String value;
        private String desc;
    
        private ErrorCode(String value, String desc) {
            this.setValue(value);
            this.setDesc(desc);
        }

    自定义项目的业务异常类:继承 RuntimeException 

    public class BusinessException extends RuntimeException {
        
        private static final long serialVersionUID = 1L;
    
        public BusinessException(Object Obj) {
            super(Obj.toString());
        }
        
    }

    自定义异常处理类:实现 HandlerExceptionResolver  这个类是用来处理异常发生时做出的逻辑处理,和spring框架集成:

    public class MySimpleMappingExceptionResolver implements HandlerExceptionResolver{
    
        @Override
        public ModelAndView resolveException(HttpServletRequest request,
                HttpServletResponse response, Object object, Exception exception) {
            // 判断是否ajax请求
            if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request
                    .getHeader("X-Requested-With") != null && request.getHeader(
                    "X-Requested-With").indexOf("XMLHttpRequest") > -1))) {
                // 如果不是ajax,JSP格式返回
                // 为安全起见,只有业务异常我们对前端可见,否则否则统一归为系统异常
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("success", false);
                if (exception instanceof BusinessException) {
                    map.put("errorMsg", exception.getMessage());
                } else {
                    map.put("errorMsg", "系统异常!");
                }
                //这里需要手动将异常打印出来,由于没有配置log,实际生产环境应该打印到log里面
                exception.printStackTrace();
                //对于非ajax请求,我们都统一跳转到error.jsp页面
                return new ModelAndView("/error", map);
            } else {
                // 如果是ajax请求,JSON格式返回
                try {
                    response.setContentType("application/json;charset=UTF-8");
                    PrintWriter writer = response.getWriter();
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("success", false);
                    // 为安全起见,只有业务异常我们对前端可见,否则统一归为系统异常
                    if (exception instanceof BusinessException) {
                        map.put("errorMsg", exception.getMessage());
                    } else {
                        map.put("errorMsg", "系统异常!");
                    }
                    JSONObject json =new JSONObject();
    //                writer.write(json.fromObject(map));
    //                writer.flush();
    //                writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    }

    在spring容器中注册这个异常处理类

    <!-- 定义异常处理器 -->
         <bean id="exceptionResolver" class="bz.beppe.exception.MySimpleMappingExceptionResolver"></bean>

    这样,在项目抛出自定义的业务异常时,都或进入这个异常处理类进行处理,然后展示给用户;

    当然项目还有一些unchecked Exception,由于代码不要求强制处理,当这些异常发生时,用户就会面对 404  500等服务器内部的页面,目前大多数服务器也都支持在Web.xml中通过<error-page>(Websphere/Weblogic)或者<error-code>(Tomcat)节点配置特定异常情况的显示页面。修改web.xml文件,增加以下内容: 

    <!-- 出错页面定义 -->  
    <error-page>  
        <exception-type>java.lang.Throwable</exception-type>  
        <location>/500.jsp</location>  
    </error-page>  
    <error-page>  
        <error-code>500</error-code>  
        <location>/500.jsp</location>  
    </error-page>  
    <error-page>  
        <error-code>404</error-code>  
        <location>/404.jsp</location>  
    </error-page>  

    这样一个健全的异常处理系统就好了;

    以下是controller层对这些业务异常和系统异常 以及 unchecked exception 的测试代码:

    public class ExceptionController {
    
        
        
            @RequestMapping(value = "/controller", method = RequestMethod.GET)  
            public void controller(Model model,String id) {  
               throw new BusinessException(ErrorCode.NULL_OBJ.toString());
            } 
            
            @RequestMapping(value = "/service", method = RequestMethod.GET)  
            public void service(Model model,String id) {  
    //           String str=null;
    //           str.substring(10);
    //           System.out.println("代码运行了");
                File file=new File("c:/a.txt");
                try {
                    FileReader reader = new FileReader(file);
                    int i = reader.read();
                    System.out.println(i);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    System.out.println("文件找不到");
                    throw new BusinessException(ErrorCode.FILE_NOT_FOUND);
    //                e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("读取文件出错");
                    e.printStackTrace();
                }
            }
           
           
    }

    至此,一个简单的完整的异常处理系统就完成了;

  • 相关阅读:
    5.9上午
    5.4下午
    5.4上午
    5.2上午
    4.19下午
    4.18上午
    4.17下午
    4.17上午
    4.12下午
    4.13下午
  • 原文地址:https://www.cnblogs.com/beppezhang/p/6397575.html
Copyright © 2020-2023  润新知