[英]Request filter not working in Jersey 2 on Embedded Jetty
I'm unable to get my request filter triggered. 我无法触发我的请求过滤器。 I run jetty 9.1 as an embedded server. 我将jetty 9.1作为嵌入式服务器运行。
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class MyFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext arg0) throws IOException {
System.out.println("HEY HO");
}
}
I registered this class in the ResourceConfig, also tried to register the package. 我在ResourceConfig中注册了这个类,也尝试注册该包。 It's just ignored. 它被忽略了。 Is it a bug, or am I missing something? 这是一个错误,还是我错过了什么?
This is how I start Jetty: 这就是我开始Jetty的方式:
Weld weld = new Weld();
WeldContainer container = weld.initialize();
URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
ResourceConfig config = ResourceConfig.forApplicationClass(MyApplication.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
(MyApplication extends ResourceConfig and calls this.package(...)
and this.register(MyFilter.class)
in the constructor. (MyApplication扩展了ResourceConfig并在构造函数中调用this.package(...)
和this.register(MyFilter.class)
。
The problem is that the class that defines the REST endpoint cannot be instantiated because - in my case - the instantiation depends on stuff that must be set up in the request filter. 问题是定义REST端点的类无法实例化,因为 - 在我的情况下 - 实例化取决于必须在请求过滤器中设置的东西。
However, the endpoint class is instanciated before the filter is called. 但是,在调用过滤器之前会对端点类进行实例化。 In order to run the filter first, it has to be annotated @PreMatching
. 为了首先运行过滤器,必须注释@PreMatching
。
@Provider
@PreMatching
public class MyFilter implements ContainerRequestFilter {
...
}
To enable cross-origin requests in a self-contained Jetty jar file with Jersey REST services (to allow calls from Swagger-UI). 在具有Jersey REST服务的自包含Jetty jar文件中启用跨源请求(允许来自Swagger-UI的调用)。 Create a CORSResponseFilter class: 创建CORSResponseFilter类:
public class CORSResponseFilter implements ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
MultivaluedMap headers = responseContext.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
//headers.add("Access-Control-Allow-Origin", "http://podcastpedia.org"); //allows CORS requests only coming from podcastpedia.org
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
}
} }
And reference it in your main App class: 并在您的主App类中引用它:
public class App {
public static void main(String[] args) throws Exception {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
ResourceConfig config = new ResourceConfig(API.class);
config.register(CORSResponseFilter.class);
Server jettyServer = JettyHttpContainerFactory.createServer(baseUri, config);
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
} }
By the similar way we have to embed the filter with jetty and jersey. 通过类似的方式,我们必须使用码头和平针织物嵌入过滤器。 hope this will use full. 希望这会充分利用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.