Spring Boot 404处理(兼容Tomcat)

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class CustomErrorController extends BasicErrorController {

    public CustomErrorController(ServerProperties serverProperties) {
        super(new DefaultErrorAttributes(), serverProperties.getError());
    }

    /**
     * 重写404错误页面
     */
    @Override
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus status = getStatus(request);
        if (status == HttpStatus.NOT_FOUND) {
            response.setStatus(HttpStatus.OK.value());
            return new ModelAndView("index");
        } else {
            return super.errorHtml(request, response);
        }
    }
}

你可能感兴趣的:(Spring Boot 404处理(兼容Tomcat))