新建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";
}
}