Jersey 2.x 自定义注入注解 NULL [英] Jersey 2.x Custom Injection annotation NULL
问题描述
我正在关注 22.1.实现自定义注入提供程序段落
I'm following the 22.1. Implementing Custom Injection Provider paragraph
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 在哪里:
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
}
}
这是我的简单注释:
@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;
在我的 web.xml 中,我通过以下配置代码部署了 Jersey servlet 容器(我错了)和 javax.ws.rs.Application:
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>
进入调试模式时,我看到在调用我的休息服务时,provide 方法从未被调用......确实始终为空.
Going into debug mode i see that when invoking my rest service the provide method is never called... indeed is always null.
我错在哪里?我正在开发 jboss Wildfly 9.0 并使用 Jersey 2.21 库
Where am i wrong? I m working on a jboss Wildfly 9.0 and using Jersey 2.21 library
推荐答案
我找到了解决方案:
1) 在 web.xml 添加:
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>
这样我就可以排除resteasy扫描我的战争.
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>
这是因为之前的部署方法是针对 Servlet 规范 3.0.
This is because the previous deployment method was for Servlet specs 3.0.
注射现在起作用了.
这篇关于Jersey 2.x 自定义注入注解 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!