Dropwizard - Filter给出了500个服务器错误,而不是定义的响应状态

时间:2013-03-05 13:48:58

标签: jersey dropwizard

我有一个过滤器 - 为我的网络服务添加了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是否为null:

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。

当我在没有startTime和endTime的情况下发出请求时,我希望看到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)

1 个答案:

答案 0 :(得分:5)

您想要在WebApplicationException上设置错误响应代码,而不是抛出ServletResponse

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