gpt4 book ai didi

java - 是否可以从 application.properties 禁用 Spring Boot 的自动异常处理

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:19 27 4
gpt4 key购买 nike

是否可以从 application.properties 禁用 Spring Boot 的自动异常处理?

我正在尝试在编码时禁用 Spring Boot 常规异常处理而不重命名 error.html,并在生产时从 application.properties 将其打开。

我从 Spring 文档中找到了以下内容:

At start-up, Spring Boot tries to find a mapping for /error. By convention, a URL ending in /error maps to a logical view of the same name: error. In the demo application this view maps in turn to the error.html Thymeleaf template. (If using JSP, it would map to error.jsp according to the setup of your InternalResourceViewResolver).

If no mapping from /error to a View can be found, Spring Boot defines its own fall-back error page - the so-called “Whitelabel Error Page” (a minimal page with just the HTTP status information and any error details, such as the message from an uncaught exception). If you rename the error.html template to, say, error2.html then restart, you will see it being used.

我还尝试了以下不起作用的属性:

server.error.path=/somewrongurl
server.error.whitelabel.enabled=false

最佳答案

将以下行添加到您的 application.properties 文件中

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

并定义您自己的异常 Controller 来捕获所有相关异常,例如

    @ControllerAdvice
public class ExceptionController {

@ExceptionHandler(value = Exception.class)
public ResponseEntity<?> handleException(Exception e) {
//handle your exception
}
}

关于java - 是否可以从 application.properties 禁用 Spring Boot 的自动异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com