在spring-boot中自定义404错误页面 [英] Customized 404 error page in spring-boot

查看:892
本文介绍了在spring-boot中自定义404错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SpringMvc(Spring-boot版本1.5.1)中为无效URL创建自定义错误页面。

I am trying to create a custom error page for invalid URL in SpringMvc (Spring-boot version 1.5.1).

为了禁用默认的白名单错误页面我有:

In order to disable the default whitelabel error page I have:

application.properties

spring.thymeleaf.cache=false
server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

我的异常处理程序是:

RestResponseEntityExceptionHandler.java

@ControllerAdvice 
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    public RestResponseEntityExceptionHandler() {
        super();
    }

    @Override
    protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
        logger.error("404 Status Code", ex);
        final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("No such page", null, request.getLocale()), "NoHandlerFound");
        return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
    }
}

原则上这样做。如果我在浏览器中访问了无效的URL,我会得到一个如下所示的JSON:

This works in principle. If I go to an invalid URL in the browser I get a JSON which looks like:


{message:没有这样的页面 ,error:NoHandlerFound}

{"message":"No such page","error":"NoHandlerFound"}

而不是JSON响应,我想显示一个适当的HTML视图(类似于白名单页面)。这应该是一个模板,我可以替换message字符串。我如何去呈现这个视图?

Instead of the JSON response I would like to show a proper HTML view (similar to the whitelabel page). This should be a template where I can replace the "message" string. How do I go about rendering this view?

推荐答案

使用Spring Boot& Spring MVC可以在resources / public下创建一个错误文件夹,并放置您的客户错误页面。 Spring将接收它们。

With Spring Boot & Spring MVC you can create an error folder under resources/public and place your customer error pages. Spring will pick them up.

src/
+- main/
   +- java/
   |   + <source code>
   +- resources/
       +- public/
           +- error/
           |   +- 404.html
           +- <other public assets>

如果您不使用Spring MVC,则必须通过实施自己的错误页面注册商。

If you're not using Spring MVC you'll have to register the error pages by implementing your own error page registrar.

@Bean
public ErrorPageRegistrar errorPageRegistrar(){
    return new MyErrorPageRegistrar();
}

private static class MyErrorPageRegistrar implements ErrorPageRegistrar {

    // Register your error pages and url paths.
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
    }

}

http://docs.spring.io / spring-boot / docs / current / reference / htmlsingle /#boot-features-error-handling-custom-error-pages

这篇关于在spring-boot中自定义404错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆