2012-06-11 37 views
5

Có thể sử dụng Guice AOP để chặn phương thức được chú thích trên tài nguyên Jersey không?Phương pháp đánh chặn tại Jersey sử dụng Guice AOP

Tôi có một cấu hình Guice được cấu hình thành công làm việc với Jersey liên quan đến Dependency Injection mà không có bất kỳ vấn đề nào, tuy nhiên Interceptor được cấu hình của tôi không chặn phương thức chú thích của tôi.

web.xml

<listener> 
    <listener-class>my.package.GuiceConfig</listener-class> 
</listener> 
<filter> 
    <filter-name>guiceFilter</filter-name> 
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>guiceFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

mô-đun cấu hình GuiceConfig

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<String, String> params = new HashMap<String, String>(); 
       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); 
      } 
     }); 
    } 
} 

RequiredAuthority chú thích

@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface RequiredAuthority { 
    String value(); 
} 

AuthorisationInterceptor khía cạnh

public class AuthorisationInterceptor implements MethodInterceptor { 

    public Object invoke(MethodInvocation methodInvocation) throws Throwable { 

     // Allow invocation to process or throw an appropriate exception 
    } 
} 

lớp tài nguyên TempResource JAX-RS

@Path("/temp") 
public class TempResource { 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    @RequiredAuthority("PERMISSION") 
    public String getTemp() { 

     // Return resource normally 
    } 
} 
+1

muộn để đảng, nhưng nó trông giống như ['@ BindingAnnotation'] (http://google-guice.googlecode.com/git/javadoc/com/google/inject/BindingAnnotation. html) cũng bị thiếu trong 'RequiredAuthority'. –

+0

Cảm ơn bạn, có điều này cũng gây ra một vấn đề tại thời điểm đó. Cũng đáng chú ý ở đây. – Kynth

Trả lời

5

Hình như configureServlets() không được gọi:

bind(TempResource.class); 
+0

Cảm ơn bạn, đó là liên kết (TempResource.class) mà tôi đã bỏ lỡ, tôi đã có ấn tượng rằng tham số PROPERTY_PACKAGES sẽ quét gói cho tài nguyên. Tôi không cần @Singleton cuối cùng. – Kynth

+0

Vấn đề tương tự có thể xảy ra với @Transactional về xử lý Google-Persist –

Các vấn đề liên quan