自定义异常
package com.as.exception;
/**
* 自定义异常
*/
public class PermissionException extends RuntimeException {
public PermissionException() {
super();
}
public PermissionException(String message) {
super(message);
}
public PermissionException(String message, Throwable cause) {
super(message, cause);
}
public PermissionException(Throwable cause) {
super(cause);
}
protected PermissionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
异常处理
package com.as.util;
import com.as.exception.PermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 全局异常处理
* @author MING
* @date 2018/8/9 10:41
*/
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {
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,返回一个自定义错误页面
if (exception instanceof PermissionException) {
JsonData result = JsonData.fail(exception.getMessage());
log.error("My page exception, url:" + request.getRequestURL().toString(), exception);
return new ModelAndView("/404", result.toMap());
} else {
JsonData result = JsonData.fail("系统异常!");
log.error("unknown page exception, url:" + request.getRequestURL().toString(), exception);
return new ModelAndView("/500", result.toMap());
}
} else {
// 如果是ajax请求,JSON格式返回
JsonData result;
if (exception instanceof PermissionException) {
result = JsonData.fail(exception.getMessage());
log.error("My json exception, url:" + request.getRequestURL().toString(), exception);
} else {
result = JsonData.fail("系统异常!");
log.error("unknown json exception, url:" + request.getRequestURL().toString(), exception);
}
ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView() ,result.toMap());
return modelAndView;
}
}
}
错误页面
这里就列一个 500 页面,404一样的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>500</title>
<style>
*{
margin: 0;
padding: 0;
}
</style>
</head>
<body style="overflow: hidden;">
<div class="img">
<img src="http://www.123.com/500.jpg" title="这是一张图片" width="100%" height="980px"/>
</div>
</body>
</html>
<———————————————————–>
因为500页面使用的是HTML格式,但有些页面使用的jsp格式,然后需要配置,可以参考这篇文章:https://blog.csdn.net/qq_38637558/article/details/82110553
当然如果你的页面全部是一个格式,这一步可以忽略
<———————————————————–>
配置异常
<!-- 配置异常处理 -->
<bean class="com.hdsdjy.util.SpringExceptionResolver"/>
测试
package com.as.controller;
import com.as.exception.ParamException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author MING
* @date 2018/8/27 12:01
*/
@Controller
@RequestMapping("")
public class TestA {
/**
* 非ajax请求
* 我们自己捕获的异常,所以会返回404 页面
* return new ModelAndView("/404", result.toMap());
* @return
*/
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
throw new ParamException("处理异常?不存在的,try catch 一步到位!");
}
/**
* 非ajax请求
* 非我们自己捕获的异常,返回500 页面
* return new ModelAndView("/500", result.toMap());
* @return
*/
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public String test2() {
throw new NullPointerException("NullPointerExceptio? try catch ");
}
/**
* json请求
* 自己捕获的异常
* @return
*/
@RequestMapping(value = "/test3", method = RequestMethod.GET)
public String test3() {
throw new ParamException("处理异常?不存在的,try catch 一步到位!");
}
}
JsonData
package com.as.util;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
/**
* json返回的数据封装
* @author MING
* @date 2018/8/9 10:41
*/
@Getter
@Setter
public class JsonData {
// 返回结果
private boolean success;
// 异常后的通知,
private String message;
// 正常的时候返回给前台的数据
private Object data;
/**
* 返回结果
* @param success
*/
public JsonData(boolean success) {
this.success = success;
}
/**
* 成功的时候,返回msg和data就够
* @param object
* @param message
* @return
*/
public static JsonData success(Object object, String message) {
JsonData jsonData = new JsonData(true);
jsonData.data = object;
jsonData.message = message;
return jsonData;
}
/**
* 成功的时候,就返回data数据
* @param object
* @return
*/
public static JsonData success(Object object) {
JsonData jsonData = new JsonData(true);
jsonData.data = object;
return jsonData;
}
/**
* 成功的时候,就返回msg说明
* @param message
* @return
*/
public static JsonData success(String message) {
JsonData jsonData = new JsonData(true);
jsonData.message = message;
return jsonData;
}
/**
* 成功的时候,不需要返回任何数据
* @return
*/
public static JsonData success() {
return new JsonData(true);
}
/**
* 失败的时候,就返回msg说明
* @param message
* @return
*/
public static JsonData fail(String message) {
JsonData jsonData = new JsonData(false);
jsonData.message = message;
return jsonData;
}
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("success", success);
result.put("message", message);
result.put("data", data);
return result;
}
}