Resteasy拦截器实现了ContainerRequestFilter。但是,下载DeploymentInfo的addFilter需要实现Filter Class。
如何解决这个问题?我们必须只写为过滤器吗?
如果是这种情况,我需要了解普通servlet上下文中的java.lang.reflect.Method吗?
答案 0 :(得分:0)
ContainerRequestFilter 是一个JAX-RS组件,必须像注册任何其他JAX-RS提供程序一样进行注册(即扫描@Provider类注释,覆盖JAX的getClasses方法 - RS应用程序,将其添加到您的web.xml上下文参数等...)
Undertow DeploymentInfo类中的 addFilter 方法具有完全不同的含义,因为它允许您将 Servlet过滤器添加到部署中。
要解决您的问题,请使用Resteasy注册 ContainerRequestFilter ,或使用 Servlet过滤器实现相同的逻辑并通过Undertow的DeploymentInfo注册
答案 1 :(得分:0)
步骤1:假定您知道如何创建Rest Service应用程序,并且具有类似的类
public class App extends javax.ws.rs.core.Application{...//set a list of services here using
@Override
public Set<Object> getSingletons(){
final Set<Object> objects=new HashSet<>();
objects.add(new MyService()));
return objects;
}
}
步骤2:创建一个标准的Filter类。这里没什么特别的。
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest,servletResponse);
}
// overwrite doInit ass well
}
步骤3:部署
FilterInfo filterInfo = new FilterInfo("MyFilter", MyFilter.class);
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setApplication(new App());
UndertowJaxrsServer restServer = new UndertowJaxrsServer();
DeploymentInfo deploymentWrapper = restServer.undertowDeployment(deployment);
deploymentWrapper.setDeploymentName("mysite.myext");
deploymentWrapper.setClassLoader(this.getClass().getClassLoader());
deploymentWrapper.setContextPath("/");
deploymentWrapper.addFilter(filterInfo);
deploymentWrapper.addFilterServletNameMapping("MyFilter","ResteasyServlet", DispatcherType.REQUEST);//Name of servlet is hardcoded in UndertowJaxrsServer class
try {
Undertow.Builder restServerBuilder = Undertow.builder()
.addHttpListener(8080, "0.0.0.0").setWorkerThreads(200);
restServer.start(restServerBuilder);
restServer.deploy(deploymentWrapper);
} catch (Exception ex) {
logger.error("We got a problem {}", ExceptionUtils.getRootCauseMessage(ex),ex);
throw ex;
}