这是一个增强的 Controller。使用这个 Controller ,可以实现三个方面的功能:
- 全局异常处理
- 全局数据绑定
- 全局数据预处理
标题全局异常处理
package com.czr.crowd.mvc.config;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.czr.crowd.util.CrowdUtil;
import com.czr.crowd.util.ResultEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import com.google.gson.Gson;
// @ControllerAdvice表示当前类是一个基于注解的异常处理器类
@ControllerAdvice
public class CrowdExceptionResolver {
@ExceptionHandler(value = ArithmeticException.class)
public ModelAndView resolveMathException(
ArithmeticException exception,
HttpServletRequest request,
HttpServletResponse response
) throws IOException {
String viewName = "error";
return commonResolve(viewName, exception, request, response);
}
@ExceptionHandler(value = NullPointerException.class)
public ModelAndView resolveNullPointerException(
NullPointerException exception,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
String viewName = "error";
return commonResolve(viewName, exception, request, response);
}
// @ExceptionHandler将一个具体的异常类型和一个方法关联起来
private ModelAndView commonResolve(
// 异常处理完成后要去的页面
String viewName,
// 实际捕获到的异常类型
Exception exception,
// 当前请求对象
HttpServletRequest request,
// 当前响应对象
HttpServletResponse response) throws IOException {
// 1.判断当前请求类型
boolean judgeResult = CrowdUtil.judgeRequestType(request);
// 2.如果是Ajax请求
if(judgeResult) {
// 3.创建ResultEntity对象
ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());
// 4.创建Gson对象
Gson gson = new Gson();
// 5.将ResultEntity对象转换为JSON字符串
String json = gson.toJson(resultEntity);
// 6.将JSON字符串作为响应体返回给浏览器
response.getWriter().write(json);
// 7.由于上面已经通过原生的response对象返回了响应,所以不提供ModelAndView对象
return null;
}
// 8.如果不是Ajax请求则创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
// 9.将Exception对象存入模型
modelAndView.addObject("exception", exception);
// 10.设置对应的视图名称
modelAndView.setViewName(viewName);
// 11.返回modelAndView对象
return modelAndView;
}
}
判断当前请求是否为Ajax请求
public class CrowdUtil {
/**
* 判断当前请求是否为Ajax请求
* @param request 请求对象
* @return
* true:当前请求是Ajax请求
* false:当前请求不是Ajax请求
*/
public static boolean judgeRequestType(HttpServletRequest request) {
// 1.获取请求消息头
String acceptHeader = request.getHeader("Accept");
String xRequestHeader = request.getHeader("X-Requested-With");
// 2.判断
return (acceptHeader != null && acceptHeader.contains("application/json"))
||
(xRequestHeader != null && xRequestHeader.equals("XMLHttpRequest"));
}
}
json统一返回
/**
* json统一返回
* @param <T>
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultEntity<T> {
public static final Integer SUCCESS = 2000;
public static final Integer FAILED = 5000;
// 返回状态码成功还是失败
private Integer code;
// 请求处理失败时返回错误消息
private String message;
// 要返回的数据
private T data;
/**
* 请求处理成功且需要返回数据
* @param data 要返回的数据
* @param <Type>
* @return
*/
public static <Type> ResultEntity<Type> successWithData(Type data){
return new ResultEntity<Type>(SUCCESS, "成功", data);
}
/**
* 请求成功不返回数据
* @param <Type>
* @return
*/
public static <Type> ResultEntity<Type> successWith(){
return new ResultEntity<Type>(SUCCESS, "成功", null);
}
/**
* 请求失败
* @param message 失败的错误信息
* @param <Type>
* @return
*/
public static <Type> ResultEntity<Type> failed(String message){
return new ResultEntity<Type>(SUCCESS, message, null);
}
}