gpt4 book ai didi

java - RuntimeException 导致 200 OK 状态

转载 作者:行者123 更新时间:2023-12-01 23:53:05 26 4
gpt4 key购买 nike

我拥有:AEM 6.4 实例。如果我的代码中的某个地方出现 RuntimeException 错误,我的页面将返回 200 OK 状态。为了解决这个问题,我尝试做Filter。

@SlingFilter( order = Integer.MIN_VALUE, scope = SlingFilterScope.REQUEST)
public class RuntimeExceptionFilter implements javax.servlet.Filter {

@Override
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

try {
chain.doFilter(request, response);
} catch (RuntimeException ex) {
slingResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}

现在我可以捕获请求,这会导致 RuntimeException,但是 slingResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

返回“java.lang.IllegalStateException:响应已提交”。可能是什么原因呢?或者可以通过其他方式让页面在发生 RuntimeException 时返回 500 Status 吗?

堆栈跟踪:

Caused by: java.lang.IllegalStateException: Response has already been committed
at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.checkCommitted(SlingHttpServletResponseImpl.java:424) [org.apache.sling.engine:2.6.16]
at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.sendError(SlingHttpServletResponseImpl.java:170) [org.apache.sling.engine:2.6.16]
at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.sendError(SlingHttpServletResponseImpl.java:165) [org.apache.sling.engine:2.6.16]
at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:127) [org.apache.felix.http.servlet-api:1.1.2]
at my.project.RuntimeExceptionFilter.doFilter(RuntimeExceptionFilter.java:49)

最佳答案

正如异常文本所示,响应已在引发异常之前发送。这里讨论了类似的问题:Response has aready been committed

首先尝试找出 HTTP 响应到底在哪个点返回。一旦你发现你可以解决它。

我对 AEM 内部不太熟悉(几乎没有实际操作经验),但很可能有一些包装器(可能是 Aspect)在某个时刻被触发并返回带有 200 的 HTTP 响应。

关于java - RuntimeException 导致 200 OK 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214474/

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