gpt4 book ai didi

jersey - Dropwizard - 过滤器给我 500 服务器错误而不是定义的响应状态

转载 作者:行者123 更新时间:2023-12-02 17:45:33 25 4
gpt4 key购买 nike

我有一个过滤器 - 像这样为我的网络服务添加了 TimeValidationFilter:

@Override
public final void run(T configuration, Environment environment) throws Exception {
environment.addFilter(new TimeValidationFilter(), "/*");
environment.manage(***blablabla***);
environment.addHealthCheck(***blablabla***);
try {
environment.addResource(new BlablaResourceImpl(configuration));
} catch (ConfigurationException e) {
throw new IllegalStateException(e);
}
}

我的 TimeValidationFilter 只是在这个阶段检查 startTime 和 endTime 是否为空:

public class TimeValidationFilter implements Filter {

public final static String START_TIME = "startTime";
public final static String END_TIME = "endTime";

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String startTimeStr = request.getParameter(START_TIME);
String endTimeStr = request.getParameter(END_TIME);

if (startTimeStr == null || endTimeStr == null) {
throw new WebApplicationException(Response.Status.BAD_REQUEST); //line 42
}
........
chain.doFilter(req, res);
}

当我正确设置 startTime 和 endTime 时,BlablaResourceImpl 的响应状态会正确返回响应代码 200、400、201。

当我在没有开始时间和结束时间的情况下发出请求时,我希望看到 400(Response.Status.BAD_REQUEST)。但是我得到 500 服务器错误和以下异常:

WARN  [2013-03-05 13:13:19,490] org.eclipse.jetty.servlet.ServletHandler: /blablabla
! javax.ws.rs.WebApplicationException: null
! at com.*******.filters.TimeValidationFilter.doFilter(TimeValidationFilter.java:42)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
! at com.yammer.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29)
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
! at com.yammer.metrics.jetty.InstrumentedHandler.handle(InstrumentedHandler.java:200)
! at org.eclipse.jetty.server.handler.GzipHandler.handle(GzipHandler.java:230)
! at com.yammer.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:123)
! at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
! at org.eclipse.jetty.server.Server.handle(Server.java:347)
! at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)
! at org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:47)
! at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)
! at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)
! at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634)
! at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)
! at org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:66)
! at org.eclipse.jetty.server.nio.BlockingChannelConnector$BlockingChannelEndPoint.run(BlockingChannelConnector.java:293)
! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
! at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
! at java.lang.Thread.run(Thread.java:722)

最佳答案

与其抛出 WebApplicationException,不如在 ServletResponse 上设置错误响应代码:

HttpServletResponse httpResponse = (HttpServletResponse) res;
if (startTimeStr == null || endTimeStr == null) {
httpResponse.sendError(400);
return;
}

关于jersey - Dropwizard - 过滤器给我 500 服务器错误而不是定义的响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15225436/

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