Jersey 2.x自定义注入注释NULL

时间:2015-09-22 18:48:06

标签: null annotations jersey-2.0 injectable

我正在关注22.1。实现自定义注入提供程序段落

https://jersey.java.net/documentation/latest/user-guide.html#deployment

我将我的课程定义如下:

public class PrincipalConfig extends ResourceConfig {   

  public PrincipalConfig() {    
    packages("com.vex.klopotest.secured,com.klopotek.klas.auth.injection");         
        register(new MyBinder());
    }
}

MyBinder的位置:

Public class MyBinder extends AbstractBinder implements Factory<KasPrincipal> {


@Override
protected void configure() {

    bindFactory(this).to(MyInjectable.class).in(RequestScoped.class);                
    bind(KasPersistenceDaoInjectionResolver.class)
         .to(new TypeLiteral<InjectionResolver<KasPersistenceDaoAnnot>>(){})
         .in(Singleton.class);

    }

    @Override
    public MyInjectable provide() {
    // TODO Auto-generated method stub
        return new MyInjectable();
    }

    @Override
    public void dispose(MyInjectable instance) {
        // TODO Auto-generated method stub

    }
}

这是我的简单注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnot {

}

我想在我的服务中使用注释:

@Path("modelORA")
public class ModelRetrieverORA {

   @Context
   SecurityContext securityContext;

    @Context
    private UriInfo uriInfo;

    @MyAnnot    
    private Myinjectable Principal;

在我的web.xml中,我通过此配置代码部署了Jersey servlet容器(我错了)和javax.ws.rs.Application:

<servlet>
    <servlet-name>com.my.package.injection.PrincipalConfig</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>com.my.package.injection.PrincipalConfig</servlet-name>
    <url-pattern>/rest</url-pattern>
</servlet-mapping>

进入调试模式我看到,在调用我的rest服务时,从不调用provide方法......实际上总是为null。

我哪里错了?我正在使用jboss Wildfly 9.0并使用Jersey 2.21库

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

1)在web.xml中添加:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
</context-param>
通过这种方式,我可以排除重新扫描我的战争。

然后使用这些部署说明:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param> 
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.klopotek.klas.auth.injection.PrincipalConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

这是因为之前的部署方法适用于Servlet规范3.0。

注射现在有效。

答案 1 :(得分:0)

这可能是因为休息网络服务实施中的冲突。你正在使用rest-ws的jersey实现,但wildfly带有resteasy(rest-ws实现)。如果您在web.xml中使用wildfly进行微小更改,我建议您继续使用resteasy。 请务必删除/评论您的球衣特定配置。

 <!-- Set this if you want Resteasy to scan for JAX-RS classes.-->
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<!-- this need same with resteasy servlet url-pattern -->
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<!--The ResteasyBootstrap listener is responsible for initializing 
some basic components of RESTeasy as well as scanning for annotation 
classes you have in your WAR file.-->
<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

您还需要从 WEB-INF / libs 目录中删除与球衣相关的罐子。