ErrorController实现错误自定404页面

553 阅读1分钟

spring自带404页面(没法用)

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jan 07 13:52:29 CST 2021
There was an unexpected error (type=Not Found, status=404).
No message available

只要实现ErrorController接口即可

@Slf4j
@ApiIgnore
@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class ErroControllerConfig implements ErrorController {

    public static final String PATH = "/erro";   //随意定义

    @Override
    public String getErrorPath() {
        return PATH;
    }


    @RequestMapping
    public R doHandleError() {
    	//前后端分离使用json返回,如果是单体可以直接跳转页面
        return R.error(404,"对不起,我迷路了");
    }
}