小编典典

独立Tomcat中的Spring Boot会忽略DeferredResults中设置的异常

tomcat

我正在独立的Tomcat中运行Spring Boot 1.2.1应用程序。

我有两个控制器映射,为简单起见,它们总是会抛出异常。第一个用于GET请求,它返回视图名称的String:

    @RequestMapping(value = {
    "/index"
}, method = RequestMethod.GET)
public String init() throws MyRequestProcessingException {
    if (true) {
    throw new MyRequestProcessingException(
            "Something went wrong processing request");
    }
    return "init";
}

这是异常定义:

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
public class MyRequestProcessingException extends Exception {

public MyRequestProcessingException(String message) {
    super(message);
}   
}

在嵌入式Tomcat和独立Tomcat中,尝试访问/ index总是导致500,并且某些JSON错误数据返回给客户端。

我的控制器有另一种方法可以接受POST并返回DeferredResult:

   @RequestMapping(value = "html/start", method = RequestMethod.POST, consumes = APPLICATION_FORM_URLENCODED)
public DeferredResult<String> start(final HttpServletResponse response,
                                    @Valid @ModelAttribute final InitialisationStartAttributes model,
                                    final SessionData sessionExisting) throws MyRequestProcessingException {
    final DeferredResult<String> finalResult = new DeferredResult<>(5000);

                // Just return an error, so we can test
                if (true) {
                    finalResult.setErrorResult(new MyRequestProcessingException(
                            "Something went wrong processing request"));
                }

    return finalResult;
}

在嵌入式Tomcat中,与其他请求方法一样,对/ html /
start的POST返回500,在响应主体中带有一些JSON数据。但是,当我尝试使用独立的Tomcat实例重现此行为时,总是收到200条响应,没有响应主体。

我在嵌入式Tomcat 8和独立的Tomcat 7.0.52中使用了Tomcat 8,但是我也尝试在独立的Tomcat 8中使用它,这没有什么不同。

通过修改/etc/tomcat/Catalina/localhost/ROOT.xml,将我的应用程序部署在根应用程序上下文中。

编辑:我做了更多的测试,并且似乎DeferredResult是元凶。我还没有重写handleErrorResult()来看看会发生什么。不过,我有点惊讶,因为我不记得在文档中看到有关在嵌入式Tomcat和独立Tomcat中返回DeferredResult之间的区别的任何信息。


阅读 408

收藏
2020-06-16

共1个答案

小编典典

此行为是由于Spring
Boot的ErrorPageFilter中的错误所致。请参阅GitHub上的错误报告。它在Spring Boot 1.2.3中已修复。

2020-06-16