普通的访问webservice 可以用
和@ExceptionHandler(Exception.class) 去捕获全局异常
@ControllerAdvice
public class MyException {
@ExceptionHandler(Exception.class)
public Object errorHander(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Exception e) throws Exception {
e.printStackTrace();
ModelAndView m = new ModelAndView();
m.addObject("error", e);
m.addObject("url", httpServletRequest.getRequestURL());
m.setViewName("error");
return m;
}
}
但是ajax的异常并不能返回视图,而是返回字符串或者json格式的数据,我们捕获完异常可以把异常信息包装起来,加上状态码和其他数据
ajax请求页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax</title>
<script type="text/javascript" src="/js/jquery-1.11.0.js"></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
type : "POST",
url : "ajax",
data : "name=John&location=Boston",
async : false,
success : function(data) {
if (data.status == 200 && data.msg == "OK") {
alert("Data Saved: " + data.msg);
}else{
alert("发生异常:"+data.msg);
}
},
error:function(){
alert("error");
}
});
</script>
</body>
</html>
自定义包装类ReaultData
package com.example.demo.pojo;
public class ResultData {
private int status;
private String msg;
private String data;
public static ResultData errorExection(String e) {
return new ResultData(555, e, null);
}
public static ResultData ok(String data) {
return new ResultData(200, "OK", data);
}
public ResultData(int status, String msg, String data) {
super();
this.status = status;
this.msg = msg;
this.data = data;
}
public ResultData() {
super();
// TODO Auto-generated constructor stub
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
自定义异常捕获
强调三次,如果这个用了@ControllerAdvice,ajax接收到的data就会是整个网页,
但是上面的普通web异常捕获中的@ControllerAdvice换成@RestControllerAdvice功能不受影响,所以后面整合两种捕获异常也用@RestControllerAdvice
@RestControllerAdvice
public class MyException {
@ExceptionHandler(Exception.class)
public Object errorHander(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Exception e) throws Exception {
e.printStackTrace();
return ResultData.errorExection(e.getMessage());
}
}
同时捕获普通的web异常和ajax的话要加if判断他们是哪种请求方式:
private boolean isAjax(HttpServletRequest request) {
return (request.getHeader("X-Requested-With") != null
&& "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
}
自定义捕获异常
@RestControllerAdvice
public class MyException {
@ExceptionHandler(Exception.class)
public Object errorHander(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Exception e) throws Exception {
e.printStackTrace();
System.out.println(isAjax(httpServletRequest));
if (isAjax(httpServletRequest)) {
return ResultData.errorExection(e.getMessage());
} else {
ModelAndView m = new ModelAndView();
m.addObject("error", e);
m.addObject("url", httpServletRequest.getRequestURL());
m.setViewName("error");
return m;
}
}
private boolean isAjax(HttpServletRequest request) {
return (request.getHeader("X-Requested-With") != null
&& "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
}
}
如果需要controller层测试,自行配置视图解析器
@Controller
public class MyContorller {
@RequestMapping("/hello")
public ModelAndView User() {
ModelAndView m=new ModelAndView();
m.setViewName("NewFile");
int i=1/0;
return m;
}
@RequestMapping("/ajax")
@ResponseBody
public ResultData ajax() {
int i=1/0;
return ResultData.ok(null);
}
}