Jersey 2.x Custom Injection annotation NULL

徘徊边缘 提交于 2019-12-12 00:18:51

问题


I'm following the 22.1. Implementing Custom Injection Provider paragraph

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

I defined my classes as below:

public class PrincipalConfig extends ResourceConfig {   

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

Where MyBinder is :

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

    }
}

and this is my simple annotation:

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

}

i wanto to use the annotation in my res service:

@Path("modelORA")
public class ModelRetrieverORA {

   @Context
   SecurityContext securityContext;

    @Context
    private UriInfo uriInfo;

    @MyAnnot    
    private Myinjectable Principal;

in my web.xml i deployed Jersey servlet container (am i wrong) and javax.ws.rs.Application by this configuration code:

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

Going into debug mode i see that when invoking my rest service the provide method is never called... indeed is always null.

Where am i wrong? I m working on a jboss Wildfly 9.0 and using Jersey 2.21 library


回答1:


i found a solution:

1) In web.xml add:

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

this way i can exclude resteasy from scanning my war.

Then use these deployment instructions:

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

This is because the previous deployment method was for Servlet specs 3.0.

The injection now works.




回答2:


It may because of conflicts in rest web-service implementation. you are using jersey implementation of rest-ws but wildfly comes with resteasy (rest-ws implementation). i suggest you to go with resteasy if you are using wildfly with minimal changes in your web.xml. Be sure to remove/comment your jersey specific configurations.

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

You also need to remove jersey related jars from your WEB-INF/libs directory.



来源:https://stackoverflow.com/questions/32724621/jersey-2-x-custom-injection-annotation-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!