SpringBoot错误页面

springboot 访问不存在的url时会返回错误信息。

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

Tue Dec 13 15:48:28 CST 2022
There was an unexpected error (type=Not Found, status=404).
No message available

错误信息中标识 ‘This application has no explicit mapping for /error’,应用没有显示的映射/error。

可以重写/error处理方法,代码如下。

@Controller
@Slf4j
public class ErrorController extends BasicErrorController {


    public ErrorController(ServerProperties serverProperties) {
        super(new DefaultErrorAttributes(), serverProperties.getError());
    }
    
    /**
     * 覆盖默认的HTML响应
     */
    @Override
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("serverExeception");
        return modelAndView;
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐