Boot achieve ErrorController interface processing error pages 404, 500, etc.

In the project we encountered a 404 Not Found error, or 500 server errors need to configure a page to a user-friendly prompts, while in Spring Boot we need how to set up.

We need to realize ErrorController interface rewrite handleError method.

package com.ciyou.edu.controller

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

import javax.servlet.http.HttpServletRequest


@Controller
class MainsiteErrorController implements ErrorController {

@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"
}

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

With the above arrangement can realize a corresponding state code jumps to the corresponding prompt page.

Related blog:

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

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

Guess you like

Origin www.cnblogs.com/dayandday/p/10960137.html