Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot ApacheHTTP服务器覆盖ApacheTomcat的4xx和5xx响应_Spring Boot_Apache_Tomcat9 - Fatal编程技术网

Spring boot ApacheHTTP服务器覆盖ApacheTomcat的4xx和5xx响应

Spring boot ApacheHTTP服务器覆盖ApacheTomcat的4xx和5xx响应,spring-boot,apache,tomcat9,Spring Boot,Apache,Tomcat9,我正在研究的项目是一个SpringBootWeb应用程序,其中请求由ApacheHTTPServer2.4转发,内容(Javaservlet和JSP)由Tomcat9提供。我遇到Apache HTTP服务器覆盖Tomcat返回的状态代码4xx和5xx的自定义错误页的问题 我已经设置了一个控制器建议类,以便在控制器方法中发生异常时将用户重定向到自定义错误页。异常处理程序方法用@ResponseStatus进一步注释,因为我希望响应的状态代码是500而不是200 /** * Controller

我正在研究的项目是一个SpringBootWeb应用程序,其中请求由ApacheHTTPServer2.4转发,内容(Javaservlet和JSP)由Tomcat9提供。我遇到Apache HTTP服务器覆盖Tomcat返回的状态代码4xx和5xx的自定义错误页的问题

我已经设置了一个控制器建议类,以便在控制器方法中发生异常时将用户重定向到自定义错误页。异常处理程序方法用@ResponseStatus进一步注释,因为我希望响应的状态代码是500而不是200

/**
 * Controller for redirecting exceptions to appropriate error page. 
 */
@ControllerAdvice
public class ErrorAdviceController {

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public String handleGenericException(Exception exception, Model model) {
    setupModel(exception, model);
    return "500";
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({RuntimeException.class, ApplicationException.class})
public String handleRuntimeException(Exception exception, Model model) {
    setupModel(exception, model);
    return "500";
}

private void setupModel(Exception exception, Model model) {
    String errorHash = RandomStringUtils.randomAlphanumeric(12);
    LOGGER.error("Exception caught ({}): {}", errorHash, exception.getMessage());
    model.addAttribute("url", request.getRequestURL());
    DateTimeFormatter exceptionDateTimeFormatter = dateTimeFormatService.getMediumDateLongTimeFormatter();
    model.addAttribute("timeStamp", exceptionDateTimeFormatter.format(DateTimeUtil.INSTANCE.nowLocal()));
    model.addAttribute("hash", errorHash);
}
为了处理控制器之外发生的未捕获异常的错误页面重定向,我在web.xml文件中为Tomcat 9定义了重定向规则

<error-page>
    <location>/errors</location>
</error-page>
尽管我的所有方法都成功命中并执行了,但Apache HTTP服务器始终将我的自定义错误页面覆盖为其默认的503服务不可用页面,并将Spring和Tomcat返回的错误状态代码从500覆盖到503

apachehttp服务器的ErrorDocument指令并不理想,因为我需要提供动态页面并在后端执行额外的处理。在过去的四天里,我一直在阅读文档和论坛,但我仍然不知道如何防止Apache HTTP服务器进行覆盖。。。我是这个行业的新手,以前从未接触过Apache和Tomcat,请帮助我

工作流
当控制器中发生运行时异常时,我的ErrorAdviceController会捕获并处理该异常,然后将响应状态代码设置为500,并告诉tomcat返回自定义的500错误页面。但是,Apache HTTP服务器随后将响应状态代码改写为503,并显示其默认的503错误页面

@Controller
public class ErrorController {
 
    @GetMapping(value = "/errors")
    public String showCustomErrorPage(Exception exception, HttpServletRequest request, Model model) {
        String errorPage= "/" + request.getAttribute("javax.servlet.error.status_code");
        String errorHash = RandomStringUtils.randomAlphanumeric(12);
        LOGGER.error("Exception caught ({}): {}", errorHash, exception.getMessage());
        
        model.addAttribute("url", request.getRequestURL());
        DateTimeFormatter exceptionDateTimeFormatter = dateTimeFormatService.getMediumDateLongTimeFormatter();
        model.addAttribute("timeStamp", exceptionDateTimeFormatter.format(DateTimeUtil.INSTANCE.nowLocal()));
        model.addAttribute("hash", errorHash);
        return errorPage;
    }
}