我有一个过滤器-为我的网络服务添加了 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。
当我在没有 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)