title: Method interception in Jersey using Guice AOP
body: <p>Is it possible to use Guice AOP to intercept an annotated method on a Jersey resource?</p>
  
  <p>I have a successfully configured Guice integration working with Jersey with respect to Dependency Injection without any problems, however my configured Interceptor is not intercepting my annotated method at all.</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;listener&gt;
      &lt;listener-class&gt;my.package.GuiceConfig&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p><strong>GuiceConfig configuration module</strong></p>
  
  <pre><code>public class GuiceConfig extends GuiceServletContextListener {
  
  @Override
  protected Injector getInjector() {
      return Guice.createInjector(new JerseyServletModule() {
  
              @Override
              protected void configureServlets() {
  
                  bindInterceptor(Matchers.any(), 
                                  Matchers.annotatedWith(RequiredAuthority.class), 
                                  new AuthorisationInterceptor());
  
                  Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); 
                  params.put(JSP_TEMPLATES_BASE_PATH, "/WEB-INF/jsp"); 
                  params.put(FEATURE_FILTER_FORWARD_ON_404, "true");
                  params.put(PROPERTY_PACKAGES, "my.service.package");
  
                  filter("/*").through(GuiceContainer.class, params);
              } 
          });
      }
  }
  </code></pre>
  
  <p><strong>RequiredAuthority annotation</strong></p>
  
  <pre><code>@Target(ElementType.METHOD)
  @Retention(RetentionPolicy.RUNTIME)
  public @interface RequiredAuthority {
      String value();
  }
  </code></pre>
  
  <p><strong>AuthorisationInterceptor aspect</strong></p>
  
  <pre><code>public class AuthorisationInterceptor implements MethodInterceptor {
  
      public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  
          // Allow invocation to process or throw an appropriate exception
      }
  }
  </code></pre>
  
  <p><strong>TempResource JAX-RS resource class</strong></p>
  
  <pre><code>@Path("/temp")
  public class TempResource {
  
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      @RequiredAuthority("PERMISSION")
      public String getTemp() {
  
          // Return resource normally
      }
  }
  </code></pre>