SpringMVC 全局異常處理代碼


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" />

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: