错误状态捕捉

时间:2022-11-20 21:20:18

错误状态捕捉:

package com.cmcc.hy.web.controller.common;

import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 错误异常状态捕捉
 */
@Controller
class MainsiteErrorController implements ErrorController {

  @GetMapping("/error")
  public String handleError(HttpServletRequest request) {
    //获取statusCode:401,404,500
    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
    if (statusCode == 404) {
      return "redirect:/web/404.html";
    } else {
      return "redirect:/web/50x.html";
    }

  }

  @Override
  public String getErrorPath() {
    return "/error";
  }
}