- 在springMVC中进行全局异常处理的配置
<bean class="cn.znx.blog.exception.MyExceptionResolver"></bean>
- 定义全局异常处理类:需要实现HandlerExceptionResolver接口
package cn.znx.blog.exception; import com.google.gson.Gson; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; public class MyExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { //打印错误信息 exception.printStackTrace(); //定义错误信息 String message = "系统繁忙,请稍后重试!"; if(exception instanceof MyException){ message = ((MyException)exception).getMessage(); } //判断请求类型 HandlerMethod handlerMethod = (HandlerMethod) handler; ResponseBody responseBody = handlerMethod.getMethod().getAnnotation(ResponseBody.class); //如果是json请求,则返回json数据 if(responseBody!=null){ Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("code", "999999"); responseMap.put("message", message); String json = new Gson().toJson(responseMap); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); try { response.getWriter().write(json); response.getWriter().flush(); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView(); } //如果是跳转页面的请求 //页面转发 ModelAndView modelAndView = new ModelAndView(); //将错误信息传到页面 modelAndView.addObject("message", message); //指向错误页面 modelAndView.setViewName("error"); return modelAndView; } }
- 自定义异常
package cn.znx.blog.exception; import com.google.gson.Gson; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; public class MyExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { //打印错误信息 exception.printStackTrace(); //定义错误信息 String message = "系统繁忙,请稍后重试!"; if(exception instanceof MyException){ message = ((MyException)exception).getMessage(); } //判断请求类型 HandlerMethod handlerMethod = (HandlerMethod) handler; ResponseBody responseBody = handlerMethod.getMethod().getAnnotation(ResponseBody.class); //如果是json请求,则返回json数据 if(responseBody!=null){ Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put("code", "999999"); responseMap.put("message", message); String json = new Gson().toJson(responseMap); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); try { response.getWriter().write(json); response.getWriter().flush(); } catch (Exception e) { e.printStackTrace(); } return new ModelAndView(); } //如果是跳转页面的请求 //页面转发 ModelAndView modelAndView = new ModelAndView(); //将错误信息传到页面 modelAndView.addObject("message", message); //指向错误页面 modelAndView.setViewName("error"); return modelAndView; } }
- 定义JSON响应类:
@Data public class Result { //状态码 private String code; //错误信息 private String message; //还是json private HashMap<String,Object> data = new HashMap<>(); //登录失败 public static Result error(String string){ Result result = new Result(); result.setCode("999999"); if (StringUtils.isEmpty(string)){ result.setMessage("登录失败"); }else{ result.setMessage(string); } return result; } //添加数据 public Result add(String key,Object value){ this.getData().put(key,value); return this; } //登录成功 public static Result success(){ Result result = new Result(); result.setCode("000000"); result.setMessage("登录成功"); return result; } }
原文地址:https://www.cnblogs.com/zhangnianxiang/p/9320660.html
时间: 2024-11-06 15:06:09