前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot2.0 添加全局异常拦截,防止详细的异常信息返回到客户端

springboot2.0 添加全局异常拦截,防止详细的异常信息返回到客户端

作者头像
zcqshine
发布2019-11-08 10:18:14
7280
发布2019-11-08 10:18:14
举报
文章被收录于专栏:zcqshine's blogzcqshine's blog

一、添加配置类:

代码语言:javascript
复制
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zcqshine
 * @date 2019/11/7
 */
@Configuration
public class ContainerConfig {
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return factory -> {
            Set<ErrorPage> set = new HashSet<>();
            set.add(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
            factory.setErrorPages(set);
        };
    }
}

二、继承并改写500异常类

代码语言:javascript
复制
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author zcqshine
 * @date 2019/11/7
 */
@Controller
public class MyBasicErrorController extends BasicErrorController {
    public MyBasicErrorController() {
        super(new DefaultErrorAttributes(), new ErrorProperties());
    }

    @RequestMapping(produces = "text/html",value = "/500")
    public ModelAndView errorHtml500(HttpServletRequest request, HttpServletResponse response) {
        response.setStatus(getStatus(request).value());
        Map<String, Object> model = getErrorAttributes(request,isIncludeStackTrace(request, MediaType.TEXT_HTML));
        model.put("msg","自定义错误信息");
        return new ModelAndView("error/500", model);
    }

    @RequestMapping(value = "/500")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> error500(HttpServletRequest request){
        Map<String,Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.APPLICATION_JSON));
        HttpStatus status = getStatus(request);
        body.put("message","系统错误");
        return new ResponseEntity<>(body,status);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档