- Create Filter class by implementing ResourceFilter and ContainerRequestFilter interfaces.
public class TestFilterFirst implements ResourceFilter, ContainerRequestFilter { @Context HttpServletRequest request; @Context HttpServletResponse response; @Override public ContainerRequest filter(ContainerRequest arg0) { //Filter logic goes here. return arg0; } @Override public ContainerRequestFilter getRequestFilter() { return this; } @Override public ContainerResponseFilter getResponseFilter() { return null; } }
2. Create Rest Service Class
@Path("/helloworld")
public class TestJersey {
@GET
@Produces("text/plain")
@ResourceFilters({ TestFilterFirst.class })
public String sayHello() {
System.out.println("Hello");
return "Hello World";
}
}
Notice ResourceFilters Annotation. multiple classes can be added here as comma separated. Each class added here will be configured as filter to this service.
No specific configuration is required in web.xml to add filters if you are using ResourceFilters annotation.