我正在将码头7迁移到码头8,
我收到此错误:


找不到适合的方法
addFilter(Class,String,int)
servletContext.addFilter(JsonTokenFilter.class,“ / *”,FilterMapping.REQUEST);


我正在尝试做的是:

 // servlet context
ServletContextHandler servletContext = new ServletContextHandler( ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);

// set session manager
SessionHandler sessionHandler = new GatewaySessionHandler(sessionManager);
servletContext.setSessionHandler(sessionHandler);
sessionHandler.addEventListener(new SessionListener());
sessionHandler.addEventListener(new CsrfGuardHttpSessionListener());
sessionManager.setSecureRequestOnly(Utilities.getConfigBoolean("fievel.jetty.secureCookies", true));
sessionManager.setHttpOnly(Utilities.getConfigBoolean(  "fievel.jetty.httpCookies", true));
setSessionSettings();

// add json rpc signal if session token invalid
servletContext.addFilter(JsonTokenFilter.class, "/*", FilterMapping.REQUEST);


最后一行是给出错误的那一行。

如果我将ServletContextHandler推荐给码头,

addFilter(FilterHolder holder, String pathSpec, EnumSet<DispatcherType> dispatches)


理想情况下,是我应该编写的代码类型。

谁能给我一个提示,我要去哪里错了?

最佳答案

我认为你应该写这样的东西:

servletContext.addFilter(JsonTokenFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));


因为它期望一个EnumSet而不是int

我已经尝试过eclipse jetty tutorial的教程

public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);

    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContext.setContextPath("/");
    server.setHandler(servletContext);

    servletContext.addServlet(new ServletHolder(new HelloServlet()),"/*");

    servletContext.addFilter(TestFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));

    server.start();
    server.join();
}


输出为:

2016-08-10 15:13:58.054:INFO:oejs.Server:jetty-8.1.19.v20160209
TestFilter Init
2016-08-10 15:13:58.186:INFO:oejs.AbstractConnector:Started [email protected]:8080
TestFilter doFilter running


因此以这种方式确保注册了过滤器

09-15 14:28