[英]Logging request with JAX-RS resteasy and ContainerRequestFilter/ContainerResponseFilter
[英]Intercept JAX-RS Request: Register a ContainerRequestFilter with tomcat
我試圖通過ContainerRequestFilter攔截對我的JAX-RS Web服務的請求。 我想將它與自定義注釋一起使用,因此我可以裝飾webservice的某些方法。 這應該使我能夠在執行實際方法之前根據信息是否在安全通道上處理來處理對此方法的請求。
我嘗試了不同的方法,搜索了幾個帖子,然后主要根據Alden在這篇文章中的答案實施。 但我無法讓它發揮作用。
我的web服務中有一個方法測試,用我的自定義注釋Ssl裝飾。
@POST
@Path("/test")
@Ssl
public static Response test(){
System.out.println("TEST ...");
}
注釋如下所示:
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Ssl {}
然后我設置了一個過濾器實現
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Ssl
@Provider
public class SslInterceptor implements ContainerRequestFilter
{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
System.out.println("Filter executed.");
}
}
但過濾器永遠不會執行,也不會出現任何錯誤消息或警告。 無論如何,測試方法運行良好。
要解決這個問題,我試圖描述在web.xml中注冊過濾器在這里 。
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.my.packagewithfilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.my.packagewithfilter.SslInterceptor</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.my.packagewithfilter</param-value>
</init-param>
</servlet>
但這也行不通。 我錯過了什么? 任何想法如何使過濾器工作? 任何幫助真的很感激!
您正在使用類中的JAX-RS 2.0 API(請求過濾器,名稱綁定等),但在您的web.xml
Jersey 1專有的init params(以com.sun.jersey
開頭的包,Jersey 2使用org.glassfish.jersey
)。 看看這個答案和這些文章:
只需編譯Michael Gajdos的答案,幫助那些不想打開更多標簽的人:
使用Jersey-2時,必須使用以下配置將過濾器注冊到web.xml中
jersey.config.server.provider.classnames
代替
com.sun.jersey.spi.container.ContainerRequestFilters (jersey-1x)
<!-- This is the config needed -->
<servlet>
//...
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>com.your_package_path.yourClassFilter</param-value>
</init-param>
//...
</servlet>
看一下這篇博客文章 ,了解更多“經典”方法(不使用注釋)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.