Spring Boot 自定义全局异常报错处理,返回 JSON/HTML


新建一个叫 GlobalErrorController 然后继承 AbstractErrorController,代码的写法可以参考  BasicErrorController 的代码。

package com.gioov.multifunction.controller.system;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.HttpStatus;
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;


/**
 * @author godcheese
 *
 */
@Controller
@RequestMapping("{server.error.path:{error.path:/error}}")
public class DefaultErrorController extends AbstractErrorController {


    public DefaultErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @Value("{server.error.path:{error.path:/error}}")
    public static final String ERROR_PATH = "/error";

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(produces = "text/html")
    public ModelAndView errorHtml(HttpServletRequest request,
                                  HttpServletResponse response) {

        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        statusCode = statusCode == null ? HttpStatus.INTERNAL_SERVER_ERROR.value() : statusCode;
        response.setStatus(getStatus(request).value());
        Map<String, Object> model = new HashMap<>();
        switch (statusCode){
            case 404:
                break;
            case 500:
                break;
            default:
                statusCode = 500;
                break;
        }
        return new ModelAndView(""+statusCode, model);
    }

    @RequestMapping
    @ResponseBody
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request,Exception exception) throws Exception {

        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        statusCode = statusCode == null ? HttpStatus.INTERNAL_SERVER_ERROR.value() : statusCode;
        Map<String, Object> body = new HashMap<>();
        HttpStatus status = getStatus(request);
        body.put("code",statusCode);
        body.put("message",HttpStatus.valueOf(statusCode));
        body.put("data",null);
        body.put("exception",exception.getMessage());
        return new ResponseEntity<>(body, status);
    }

}

然后在 resources/templates 目录下新建 404.html、500.html、error.html 等几个文件,然后访问不存在的链接就可以访问到自定义的报错页面了,如果是json请求还可以返回自定义的json内容。


《“Spring Boot 自定义全局异常报错处理,返回 JSON/HTML”》 有 2 条评论

  1. spring boot 异常处理在本地正常可以跳转到自定义的错误页面,打包放到外部tocmat就不行了,求解决办法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注