1 注册错误页面
代码语言:javascript复制@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/404");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
registry.addErrorPages(error400Page,error404Page,error500Page);
}
}
2controller进行拦截
然后你只需要写个controller拦截不同请求然后跳到不同的自定义错误页面即可,如下所示:
代码语言:javascript复制@RequestMapping("/error/{status}")
public String errorPage(@PathVariable Integer status){
switch (status){
case 401:
case 400:return "/error/404";
case 500:return "/error/500";
default:return "/error/default";
}
}
对应的404 500页面是你自己写的