【问题标题】:What is exactly server.error.path property?究竟什么是 server.error.path 属性?
【发布时间】:2018-12-26 06:53:07
【问题描述】:

在Spring Boot中,application.properties文件中server.error.path属性的作用是什么?

文档只是说:

错误控制器的路径

但我想用一个例子来清楚地描述这个属性。

【问题讨论】:

    标签: spring spring-boot configuration properties-file


    【解决方案1】:

    server.error.path - 用作错误页面 url 的一部分。

    site.getBaseUrl() + "/error"
    

    例如,服务器端发生了一些错误,您决定将用户重定向到错误页面,如下所示:

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png

    您可以在此处找到错误控制器的代码示例:

    https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html

    您可以在@RequestMapping("/error") 中使用此属性。但是你可以使用"${server.error.path}"而不是"/error"

    更新:

    另外,Spring Boot BasicErrorController 使用 server.error.path 属性

    【讨论】:

      【解决方案2】:

      Spring Boot 应用程序中的属性server.error.path 用于在处理自定义错误处理程序时定义错误路径。在 Spring 中,我们使用函数式接口 ErrorController 创建自定义错误处理程序,该接口有一个 String 类型的方法 getErrorPath 帮助我们返回错误页面路径(我们的错误页面作为视图)。 但是从 Spring 2.3.0 开始,这个 getErrorPath() 方法已被弃用并替换为 server.error.path 以管理错误路径。

      例如server.error.path=/error

      关于接口ErrorController的更多细节,请参考Spring doc for ErrorController

      【讨论】:

        猜你喜欢
        • 2014-06-27
        • 1970-01-01
        • 2014-03-11
        • 2012-07-17
        • 2012-10-28
        • 2014-10-28
        • 2012-08-27
        • 2010-11-12
        • 2011-03-18
        相关资源
        最近更新 更多