0

我正在关注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 {

}

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

@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>

进入调试模式,我看到在调用我的休息服务时,永远不会调用提供方法......确实总是为空。

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

4

2 回答 2

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>

这样我就可以将resteasy排除在扫描我的战争之外。

然后使用这些部署说明:

<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。

注射现在起作用了。

于 2015-09-23T09:04:55.353 回答
0

这可能是因为 REST Web 服务实现中的冲突。您正在使用 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目录中删除与球衣相关的 jar。

于 2015-09-22T19:42:32.197 回答