Boot 实现ErrorController接口处理40四、500等错误页面

在项目中咱们遇到404找不到的错误、或者500服务器错误都须要配置相应的页面给用户一个友好的提示,而在Spring Boot中咱们须要如何设置。java

咱们须要实现ErrorController接口,重写handleError方法。web

package com.ciyou.edu.controllerspring

import org.springframework.boot.autoconfigure.web.ErrorController
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping服务器

import javax.servlet.http.HttpServletRequestapp


@Controller
class MainsiteErrorController implements ErrorController {ide

@RequestMapping("/error")
public String handleError(HttpServletRequest request){
//获取statusCode:401,404,500
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code")
if(statusCode == 401){
return "/401"
}else if(statusCode == 404){
return "/404"
}else if(statusCode == 403){
return "/403"
}else{
return "/500"
}.net

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

经过上述设置就能够实现对应状态码跳转到对应的提示页面了。code

相关博客:blog

https://blog.csdn.net/loongshawn/article/details/50915979接口

https://blog.csdn.net/linzhiqiang0316/article/details/52600839

相关文章
相关标签/搜索