Java 捕获thymeleaf异常时,Spring引导重定向到错误页面
当Thymeleaf错误发生时(即片段不存在),我想重定向到我的自定义错误页(/error/page) 我的问题是,如果有任何错误,整个错误页面都会被呈现到Thymeleaf片段中 我试过编写一个自定义错误处理程序和一个自定义错误控制器,它可以很好地处理任何其他异常(重定向到我的自定义错误页面),但在发生Thymeleaf错误时就不行了Java 捕获thymeleaf异常时,Spring引导重定向到错误页面,java,spring,spring-boot,exception,thymeleaf,Java,Spring,Spring Boot,Exception,Thymeleaf,当Thymeleaf错误发生时(即片段不存在),我想重定向到我的自定义错误页(/error/page) 我的问题是,如果有任何错误,整个错误页面都会被呈现到Thymeleaf片段中 我试过编写一个自定义错误处理程序和一个自定义错误控制器,它可以很好地处理任何其他异常(重定向到我的自定义错误页面),但在发生Thymeleaf错误时就不行了 @Controller public class CustomErrorController implements ErrorControlle
@Controller
public class CustomErrorController implements ErrorController {
private final String ERROR_PATH = "/error";
@RequestMapping(ERROR_PATH)
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
int statusCode = Integer.parseInt(status.toString());
if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "redirect:/error/page";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "redirect:/error/page";
}
}
// Thymeleaf error -> status code 200 and doesn't work redirect
return "redirect:/error/page";
}
@Override
public String getErrorPath() {
return ERROR_PATH;
}
@ModelAttribute(name = "url")
public String getUrl(@AuthenticationPrincipal User user) {
return user != null
? "rooms"
: "";
}
}
Application.properties:
############## ERROR PAGE ################
server.error.whitelabel.enabled=false
server.error.path=/error
@启用自动配置
AppConfig.java
@Bean
public FilterRegistrationBean<ThymeleafErrorFilter> thymeleafErrorFilter() {
var thymeleafErrorFilter = new FilterRegistrationBean<ThymeleafErrorFilter>();
thymeleafErrorFilter.setName("thymeleafErrorFilter");
thymeleafErrorFilter.setFilter(new ThymeleafErrorFilter());
thymeleafErrorFilter.addUrlPatterns("/*");
return thymeleafErrorFilter;
}
@Bean
公共过滤器注册bean thymeleafErrorFilter(){
var thymeleafErrorFilter=新的FilterRegistrationBean();
setName(“thymeleafErrorFilter”);
setFilter(新的thymeleafErrorFilter());
thymeleafErrorFilter.addUrlPatterns(“/*”);
返回错误过滤器;
}
如果胸腺肽片段不存在。
我们在胸腺滤过器中检测到它。在spring调用/错误(我们在CustomErrorController中捕捉到)和CustomErrorController中,状态代码为200,重定向不起作用。您应该使用@ControllerAdvise处理异常并提供一个视图来呈现错误 见此:
@ControllerAdvice
@Slf4j
public class ErrorControllerAdvice {
@ExceptionHandler(StorageException.class) //handle this exception
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String storageException(final StorageException throwable, final Model model) {
log.error("Exception during execution of application", throwable);
model.addAttribute("errorMessage", "Failed to store file"); //custom message to render in HTML
return "error"; //the html page in resources/templates folder
}
/// handle other exeptions
视图:
@ControllerAdvice
@Slf4j
public class ErrorControllerAdvice {
@ExceptionHandler(StorageException.class) //handle this exception
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String storageException(final StorageException throwable, final Model model) {
log.error("Exception during execution of application", throwable);
model.addAttribute("errorMessage", "Failed to store file"); //custom message to render in HTML
return "error"; //the html page in resources/templates folder
}
/// handle other exeptions
error.html
<h1> Opps.. Sorry about this. </h1>
<p th:utext="${errorMessage}"></p>
Opps。。很抱歉。
有关更多详细信息,请参阅。您应该使用@ControllerAdvise来处理异常,并提供一个视图来呈现错误 见此:
@ControllerAdvice
@Slf4j
public class ErrorControllerAdvice {
@ExceptionHandler(StorageException.class) //handle this exception
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String storageException(final StorageException throwable, final Model model) {
log.error("Exception during execution of application", throwable);
model.addAttribute("errorMessage", "Failed to store file"); //custom message to render in HTML
return "error"; //the html page in resources/templates folder
}
/// handle other exeptions
视图:
@ControllerAdvice
@Slf4j
public class ErrorControllerAdvice {
@ExceptionHandler(StorageException.class) //handle this exception
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String storageException(final StorageException throwable, final Model model) {
log.error("Exception during execution of application", throwable);
model.addAttribute("errorMessage", "Failed to store file"); //custom message to render in HTML
return "error"; //the html page in resources/templates folder
}
/// handle other exeptions
error.html
<h1> Opps.. Sorry about this. </h1>
<p th:utext="${errorMessage}"></p>
Opps。。很抱歉。
有关更多详细信息,请参阅。@ControllerAdvice批注无法工作,因为您只能在控制器内部将其用于异常,而此处您希望在视图中处理异常。
您只能在控制器内部将其用于异常。
这是什么意思?为了澄清,@ControllerAdvice
处理控制器方法引发的异常。因此,这里您希望在视图中处理异常。
否。我没有在视图中处理异常。我只是在视图中显示消息。如果我收到thymeleaf异常,则@ExceptionHandling
无法捕获它。因此,当应用程序到达控制器时,@ExcaptionHandling
可以捕获它们。重定向到错误页面很重要,因为如果我不重定向到错误页面,thymeleaf将使用错误页面部分呈现整个页面。@ControllerAdvice批注无法工作,因为您只能在控制器内部将其用于异常,在这里,您希望在视图中处理异常。您只能在控制器内部对异常使用它
这是什么意思?为了澄清,@ControllerAdvice
处理控制器方法引发的异常。因此,这里您希望在视图中处理异常。
否。我没有在视图中处理异常。我只是在视图中显示消息。如果我收到thymeleaf异常,则@ExceptionHandling
无法捕获它。因此,当应用程序到达控制器时,@ExcaptionHandling
可以捕获它们。重定向到错误页面很重要,因为如果我不重定向到错误页面,那么thymeleaf将使用错误页面部分呈现整个页面。