新建MyErrorController(名字随意)类继承ErrorController接口,并实现getErrorPath方法:
package com.example.testssm.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
public class MyErrorController implements ErrorController {
@RequestMapping(value = "/error", method = {RequestMethod.GET, RequestMethod.POST})
public String Error(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
switch (statusCode) {
case 403:
return "403";
default:
return "404";
}
}
@Override
public String getErrorPath() {
return "/error";
}
}

本文介绍如何在Spring Boot应用中创建自定义错误控制器MyErrorController,通过实现ErrorController接口并覆盖getErrorPath方法,实现根据不同HTTP状态码返回特定错误页面的功能。
3492

被折叠的 条评论
为什么被折叠?



