Krishna Chaitanya
Krishna Chaitanya

Reputation: 2663

Jax ws rs filter

I am trying to write a filter to filter calls to my web services. I am using WAS 8.5 java ee 6 compilant application server.

I searched online and found that there are two interfaces ContainerRequestFilter and ContainerResponseFilter. I tried to implement these interfaces and found that these interfaces are not part of javaee 6 jax ws rs specification.

Can any one please tell me how do I filter the requests and responses in a java ee 6 environment.

We are using jersey as the jax ws rs implementation. I don't want to use any jersey specific classes because we want to migrate the code to JBoss, where we will use rest easy as the implementation.

Upvotes: 0

Views: 1558

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

The specific Java EE 6 way is through implementing the javax.servlet.Filter interface and then annotating your class with the javax.servlet.annotation.WebFilter annotation.

You can see the docs right here:

https://docs.oracle.com/javaee/6/tutorial/doc/bnagb.html

Here's the relevant snippet:

import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

@WebFilter(filterName = "TimeOfDayFilter",
urlPatterns = {"/*"},
initParams = {
    @WebInitParam(name = "mood", value = "awake")})
public class TimeOfDayFilter implements Filter {
    ....

However, I don't see any reason why implementing ContainerRequestFilter or ContainerResponseFilter and registering it in your web.xml wouldn't work just as well.

Upvotes: 1

Related Questions