0

I followed the 22.1. Implementing Custom Injection Provider paragraph

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

I was able to develop my own injectable custom annotation following the steps described in this post:

Jersey 2.x Custom Injection annotation NULL

So i use these deployment instructions to bind my injectionresolver:

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

Now i'm able to get my object injected into my custom annotation inside my rest service:

@Path("modelORA")
public class ModelRetrieverORA {

   @Context
   SecurityContext securityContext;

   @Context
   private UriInfo uriInfo;

   @MyAnnot    
   private Myinjectable Principal;

Both @Context and @MyAnnot works fine as @Context annotation is handled inside Jersey Container.

Now i want to use JPA and JTA for my persistence layer. My final code should look like:

 @Path("modelORA")
public class ModelRetrieverORA {

   @PersistenceContext (unitname="myName")
   EntityManager manager;

   @Resource
   UserTransaction transaction;

   @Context
   SecurityContext securityContext;

   @Context
   private UriInfo uriInfo;

   @MyAnnot    
   private Myinjectable Principal;

But of course both manager and transaction are null. The injection doesn't happen.

I think the issue is related to either the HK2 injection engine used by Jersey2 (i need it because of my custom annotation) or CDI engine in my application server (tomcat or wildfly 9). So how can i say to the WELD or whatever CDI engine to inject transaction and JPA manager?

Community
  • 1
  • 1
Alex
  • 1,515
  • 2
  • 22
  • 44
  • Try adding @Stateless annotation at the beginning of your ws class, above @Path("modelORA") annotation. – Javier Haro Sep 27 '15 at 22:18
  • Thanks for answering... i did just now but nothing has changed ...@Stateless(name ="RestServiceModelORA") @TransactionManagement(TransactionManagementType.BEAN) ... stil null value – Alex Sep 28 '15 at 08:21
  • What server are you running your application at? – Javier Haro Sep 28 '15 at 11:57
  • I m running on Wildfly 9 final – Alex Sep 28 '15 at 12:07
  • I used it in one occasion... You can read [this link](http://blog.denevell.org/java-jersey-dependency-injection.html). And there is more resources in internet in the same direction if you search by "HK2 Jersey entitymanager injection". – Javier Haro Sep 28 '15 at 20:27
  • Thanks a lot for your comment... actually i alredy saw this link but i was wonderingif it s the only way to make the injection to work. Why isn t sufficient just to annotate the resource service as stateless? – Alex Sep 28 '15 at 20:31
  • If you annotate your class as stateless you would have an EJB 3.1 bean, so you ought to be able to inyect JPA entity bean in EJB. You can see several examples in internet. But I think I used the standard JAX-RS 2.0. – Javier Haro Sep 28 '15 at 20:48

0 Answers0