首页 > 编程语言 > 详细

SpringMVC 全局异常处理代码

时间:2015-10-31 21:38:24      阅读:247      评论:0      收藏:0      [点我收藏+]

SpringMVC 全局异常处理代码
封装的实体类:

package com.sun4j.app.exception;
/**
 * 状态码和消息实体类
 * @author Sunlight
 *
 */
public class CodeMessage {
    private String code;
    private String message;

    public CodeMessage() {
    }

    public CodeMessage(String code, String message) {
        this.setCode(code);
        this.setMessage(message);
    }

    public String getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

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

系统异常类:

package com.sun4j.app.exception;

/**
 * 系统异常类
 * 
 * @author Sunlight
 * 
 */
public class MyException extends RuntimeException {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static final MyException SUCCESS = new MyException("0", "成功");
    public static final MyException SERVER_ERROR = new MyException("-1",
            "服务器异常");
    public static final MyException PERMISSION_DENIED = new MyException("-2",
            "您的权限不足");
    public static final MyException CONNECTION_TIMEOUT = new MyException("-3",
            "网络连接异常");
    public static final MyException CONNECTION_ERROR = new MyException("-4",
            "连接超时");

    private String code;
    private String message;

    public MyException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public MyException(CodeMessage cm) {
        this.code = cm.getCode();
        this.message = cm.getMessage();
    }

    public String getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

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

    public CodeMessage getCodeMessage() {
        return new CodeMessage(code, message);
    }

}

异常处理类:

package com.sun4j.app.exception;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
/**
 * 异常处理类
 * @author Sunlight
 *
 */
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception e) {
        e.printStackTrace();
        ModelAndView mv = new ModelAndView();
        CodeMessage cm = null;
        if (e instanceof MyException) {
            cm = new CodeMessage(((MyException) e).getCode(), e.getMessage());
        } else {
            cm = new CodeMessage("500", e.toString());
        }
        mv.addObject("exception", cm);
        mv.addObject("resultCode", cm.getCode());
        mv.addObject("resultMessage", "未知错误");
        mv.setViewName("/err/500");
        HandlerMethod handlerMethod=(HandlerMethod) handler;
        ResponseBody body=handlerMethod.getMethodAnnotation(ResponseBody.class);
        if (body!=null) {
            try {
                //返回json数据
                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
                response.setCharacterEncoding("UTF-8");
                response.setHeader("Cache-Control","no-cache, must-revalidate");
                PrintWriter writer=response.getWriter();
                writer.write(JSONObject.toJSONString(mv.getModel()));
                writer.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return mv;
    }
}

spring.xml 文件中的配置

<!-- 全局异常处理 -->
<bean id="exceptionResolver" class="com.sun4j.app.exception.MyHandlerExceptionResolver" />

版权声明:本文为博主原创文章,未经博主允许不得转载。

SpringMVC 全局异常处理代码

原文:http://blog.csdn.net/omsvip/article/details/49533193

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!