1. 程式人生 > 實用技巧 >全域性異常處理--page和json介面區分處理

全域性異常處理--page和json介面區分處理

import com.alibaba.fastjson.JSON;
import com.shsxt.crm.model.ResultInfo;
import org.springframework.stereotype.Component;
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.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

/**
 * Created by lyf on 2019/7/15.
 */
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object target, Exception ex) {

        ModelAndView mv = createDefaultModelAndView(request, ex);

        /***
         * 區分: 普通page頁面請求和json資料請求?
         * 通過@ResponseBody註解區分
         * */

        if(target instanceof HandlerMethod){
            System.out.println(target);
            HandlerMethod handlerMethod = (HandlerMethod) target;
            Method method = handlerMethod.getMethod();
            ResponseBody responseBody = method.getAnnotation(ResponseBody.class);
            if(null==responseBody){
                if(ex instanceof ParamsException){
                    ParamsException e = (ParamsException) ex;
                    mv.setViewName("error");// 可以單獨設定
                    mv.addObject("errorMsg", e.getMsg());
                }
            }else{
                ResultInfo info = new ResultInfo();
                info.setCode(300);// 預設錯誤碼
                info.setMsg("系統繁忙");// 預設錯誤資訊

                if(ex instanceof ParamsException){
                    ParamsException e = (ParamsException) ex;
                    info.setCode(e.getCode());
                    info.setMsg(e.getMsg());
                }

                /**
                 * 響應JSON到前臺
                 */

                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json;charset=utf-8");
                PrintWriter pw =null;

                try {
                    pw = response.getWriter();
                    pw.write(JSON.toJSONString(info));
                    pw.flush();
                    pw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(null!=pw){
                        pw.close();
                    }
                }
                return null;
            }
        }
        return mv;
    }

    private ModelAndView createDefaultModelAndView(HttpServletRequest request, Exception ex) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("error");
        mv.addObject("errorMsg", ex.getMessage());
        return mv;
    }
}