我想将ContainerResponseFilter和ContainerRequestFilter添加到我的jetty-server。但是当我尝试添加它时,我得到的错误是该类不好。
我的码头服务器设置:
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addFilter(CorsFilter.class, //Error here
"/*", EnumSet.of(DispatcherType.REQUEST));
Server jettyServer = new Server(8090);
jettyServer.setHandler(context);
ServletHolder jerseyServlet =
context.addServlet(ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
Login.class.getCanonicalName());
Starter.start(jettyServer);
和我的过滤器:
public class CorsFilter implements ContainerRequestFilter, ContainerResponseFilter {
错误是该方法未解决。谢谢!
ContainerRequestFilter
和ContainerResponseFilter
不是Servlet过滤器,它们是JAX-RS过滤器。
将它们注册到ResourceConfig实现中。
例:
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
// Register resources and providers using package-scanning.
packages("my.package");
// Register my custom provider - not needed if it's in my.package.
register(CorsFilter.class);
// Register an instance of LoggingFilter.
register(new LoggingFilter(LOGGER, true));
...
}
}