zoukankan      html  css  js  c++  java
  • Spring Boot--01错误处理

    package com.smartmap.sample.ch1.controller.view;
    
    import java.io.IOException;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.web.ErrorProperties;
    import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
    import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
    import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
    import org.springframework.boot.web.servlet.error.ErrorAttributes;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @Controller
    public class ErrorViewController extends AbstractErrorController {
        final Log log = LogFactory.getLog(ErrorViewController.class);
        ErrorProperties errorProperties;
    
        @Autowired
        ObjectMapper objectMapper;
    
        public ErrorViewController() {
            super(new DefaultErrorAttributes());
        }
    
        public ErrorViewController(ErrorAttributes errorAttributes, ErrorProperties errorProperties,
                List<ErrorViewResolver> errorViewResolvers) {
            super(errorAttributes, errorViewResolvers);
            this.errorProperties = errorProperties;
        }
    
        @Override
        public String getErrorPath() {
            return errorProperties.getPath();
        }
    
        @RequestMapping("/error")
        public ModelAndView getErrorInfo(HttpServletRequest request, HttpServletResponse response) {
    
            Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, false));
    
            Throwable cause = this.getCause(request);
            int status = (Integer) model.get("status");
            String message = (String) model.get("message");
            String errorMessage = getErrorMessage(cause);
            log.info(status + "," + message, cause);
            response.setStatus(status);
            if (!isJsonRequest(request)) {
                ModelAndView modelAndView = new ModelAndView();
                modelAndView.addAllObjects(model);
                modelAndView.addObject("errorMessage", errorMessage);
                modelAndView.addObject("status", status);
                modelAndView.addObject("cause", cause);
                return modelAndView;
            } else {
                Map<String, Object> error = new HashMap();
                error.put("errorMessage", errorMessage);
                error.put("status", status);
                error.put("cause", cause);
    
                writeJson(response, error);
                return null;
    
            }
        }
    
        protected Throwable getCause(HttpServletRequest request) {
            Throwable error = (Throwable) request.getAttribute("javax.servlet.error.exception");
            if (error != null) {
                while (error instanceof ServletException && error.getCause() != null) {
                    error = ((ServletException) error).getCause();
                }
            }
    
            return error;
    
        }
    
        protected void writeJson(HttpServletResponse response, Map error) {
            response.setContentType("application/json;charset=utf-8");
            try {
                response.getWriter().write(objectMapper.writeValueAsString(error));
            } catch (IOException e) {
                // ignore
            }
        }
    
        protected String getErrorMessage(Throwable ex) {
            /* 不给前端显示详细错误 */
            return "服务器错误,请联系管理员";
        }
    
        protected boolean isJsonRequest(HttpServletRequest request) {
            return request.getHeader("Accept").contains("application/json");
        }
    
    }
  • 相关阅读:
    JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{&quot;options&quot;:&quot;[{
    CLLocation的属性以及使用的解释
    单片机小白学步系列(十六) 单片机/计算机系统概述:模块化思想
    关于android中的单位(dp、sp)
    手动脱RLPack壳实战
    集成环信时遇到的问题file not found: libEaseMobClientSDK.a
    Cocos2dx 小技巧(九)现成的粒子特效
    设计模式
    2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)
    hdu 2544 最短路(SPFA算法)
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/8920876.html
Copyright © 2011-2022 走看看