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

相關文章
相關標籤/搜索