CDI failure with Dependent/applicatio Scope class












0














I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.



java.lang.IllegalStateException: No CXF message usable for JAX-RS @Context injections in that thread so can't use interface javax.servlet.ServletContext


I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:



@Dependent
public class AuthContexthandler {

@Context
ServletContext sc;

@Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}









share|improve this question
























  • I think, if you Use inject annotion instead of context annotation, it will work
    – Mehran Mastcheshmi
    Nov 22 at 16:14










  • Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
    – Freak
    Nov 22 at 16:59






  • 1




    When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
    – Christoph Böhme
    Nov 22 at 17:20










  • @ChristophBöhme makes sense now !! Well explained.
    – Freak
    Nov 23 at 11:45
















0














I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.



java.lang.IllegalStateException: No CXF message usable for JAX-RS @Context injections in that thread so can't use interface javax.servlet.ServletContext


I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:



@Dependent
public class AuthContexthandler {

@Context
ServletContext sc;

@Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}









share|improve this question
























  • I think, if you Use inject annotion instead of context annotation, it will work
    – Mehran Mastcheshmi
    Nov 22 at 16:14










  • Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
    – Freak
    Nov 22 at 16:59






  • 1




    When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
    – Christoph Böhme
    Nov 22 at 17:20










  • @ChristophBöhme makes sense now !! Well explained.
    – Freak
    Nov 23 at 11:45














0












0








0







I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.



java.lang.IllegalStateException: No CXF message usable for JAX-RS @Context injections in that thread so can't use interface javax.servlet.ServletContext


I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:



@Dependent
public class AuthContexthandler {

@Context
ServletContext sc;

@Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}









share|improve this question















I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.



java.lang.IllegalStateException: No CXF message usable for JAX-RS @Context injections in that thread so can't use interface javax.servlet.ServletContext


I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:



@Dependent
public class AuthContexthandler {

@Context
ServletContext sc;

@Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}






dependency-injection jax-rs cdi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:46









Kukeltje

8,76641338




8,76641338










asked Nov 22 at 14:21









Freak

5,56842844




5,56842844












  • I think, if you Use inject annotion instead of context annotation, it will work
    – Mehran Mastcheshmi
    Nov 22 at 16:14










  • Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
    – Freak
    Nov 22 at 16:59






  • 1




    When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
    – Christoph Böhme
    Nov 22 at 17:20










  • @ChristophBöhme makes sense now !! Well explained.
    – Freak
    Nov 23 at 11:45


















  • I think, if you Use inject annotion instead of context annotation, it will work
    – Mehran Mastcheshmi
    Nov 22 at 16:14










  • Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
    – Freak
    Nov 22 at 16:59






  • 1




    When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
    – Christoph Böhme
    Nov 22 at 17:20










  • @ChristophBöhme makes sense now !! Well explained.
    – Freak
    Nov 23 at 11:45
















I think, if you Use inject annotion instead of context annotation, it will work
– Mehran Mastcheshmi
Nov 22 at 16:14




I think, if you Use inject annotion instead of context annotation, it will work
– Mehran Mastcheshmi
Nov 22 at 16:14












Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
– Freak
Nov 22 at 16:59




Yes, It did. Post it as a answer but Can you please explain why is it so? I am lost all in CDI concepts !!
– Freak
Nov 22 at 16:59




1




1




When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
– Christoph Böhme
Nov 22 at 17:20




When I learned CDI and JAX-RS I was also quite confused by the dependency injection (DI) concepts until I released that JAX-RS has its own DI mechanism which uses not the same annotations as CDI. This results in applications having beans that are managed by JAX-RS and other's which are managed by CDI. As both frameworks use different annotations for marking beans and injection points you need to make sure that you use the correct annotations. In your example the class AuthContextHandler is a CDI bean (because of the @Dependent annotaion) while @Context marks a JAX-RS injection point.
– Christoph Böhme
Nov 22 at 17:20












@ChristophBöhme makes sense now !! Well explained.
– Freak
Nov 23 at 11:45




@ChristophBöhme makes sense now !! Well explained.
– Freak
Nov 23 at 11:45












1 Answer
1






active

oldest

votes


















1














you must use @inject instead of @Context.



you can use @Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as @Christoph Böhme said) is not a JAX-RS source class so you cannot use @Context



but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:




An object bound to a lifecycle context is called a bean. CDI includes
built-in support for several different kinds of bean, including the
following Java EE component types:



managed beans, and EJB session beans. Both managed beans and EJB
session beans may inject other beans. But some other objects, which
are not themselves beans in the sense used here, may also have beans
injected via CDI. In the Java EE platform, the following kinds of
component may have beans injected:



message-driven beans,



interceptors,



servlets,



servlet filters and



servlet event listeners,



JAX-WS service endpoints and handlers,



JAX-RS resources,



providers and javax.ws.rs.core.Application subclasses, and



JSP tag handlers and tag library event listeners.




it means you also can use @Inject annotation in your JAX-RS source class.



there are also some predefined Beans in CDI such as ServletContext that you can use @inject annotation to inject them.



https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432978%2fcdi-failure-with-dependent-applicatio-scope-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    you must use @inject instead of @Context.



    you can use @Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as @Christoph Böhme said) is not a JAX-RS source class so you cannot use @Context



    but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:




    An object bound to a lifecycle context is called a bean. CDI includes
    built-in support for several different kinds of bean, including the
    following Java EE component types:



    managed beans, and EJB session beans. Both managed beans and EJB
    session beans may inject other beans. But some other objects, which
    are not themselves beans in the sense used here, may also have beans
    injected via CDI. In the Java EE platform, the following kinds of
    component may have beans injected:



    message-driven beans,



    interceptors,



    servlets,



    servlet filters and



    servlet event listeners,



    JAX-WS service endpoints and handlers,



    JAX-RS resources,



    providers and javax.ws.rs.core.Application subclasses, and



    JSP tag handlers and tag library event listeners.




    it means you also can use @Inject annotation in your JAX-RS source class.



    there are also some predefined Beans in CDI such as ServletContext that you can use @inject annotation to inject them.



    https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
    https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm






    share|improve this answer


























      1














      you must use @inject instead of @Context.



      you can use @Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as @Christoph Böhme said) is not a JAX-RS source class so you cannot use @Context



      but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:




      An object bound to a lifecycle context is called a bean. CDI includes
      built-in support for several different kinds of bean, including the
      following Java EE component types:



      managed beans, and EJB session beans. Both managed beans and EJB
      session beans may inject other beans. But some other objects, which
      are not themselves beans in the sense used here, may also have beans
      injected via CDI. In the Java EE platform, the following kinds of
      component may have beans injected:



      message-driven beans,



      interceptors,



      servlets,



      servlet filters and



      servlet event listeners,



      JAX-WS service endpoints and handlers,



      JAX-RS resources,



      providers and javax.ws.rs.core.Application subclasses, and



      JSP tag handlers and tag library event listeners.




      it means you also can use @Inject annotation in your JAX-RS source class.



      there are also some predefined Beans in CDI such as ServletContext that you can use @inject annotation to inject them.



      https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
      https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm






      share|improve this answer
























        1












        1








        1






        you must use @inject instead of @Context.



        you can use @Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as @Christoph Böhme said) is not a JAX-RS source class so you cannot use @Context



        but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:




        An object bound to a lifecycle context is called a bean. CDI includes
        built-in support for several different kinds of bean, including the
        following Java EE component types:



        managed beans, and EJB session beans. Both managed beans and EJB
        session beans may inject other beans. But some other objects, which
        are not themselves beans in the sense used here, may also have beans
        injected via CDI. In the Java EE platform, the following kinds of
        component may have beans injected:



        message-driven beans,



        interceptors,



        servlets,



        servlet filters and



        servlet event listeners,



        JAX-WS service endpoints and handlers,



        JAX-RS resources,



        providers and javax.ws.rs.core.Application subclasses, and



        JSP tag handlers and tag library event listeners.




        it means you also can use @Inject annotation in your JAX-RS source class.



        there are also some predefined Beans in CDI such as ServletContext that you can use @inject annotation to inject them.



        https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
        https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm






        share|improve this answer












        you must use @inject instead of @Context.



        you can use @Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as @Christoph Böhme said) is not a JAX-RS source class so you cannot use @Context



        but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:




        An object bound to a lifecycle context is called a bean. CDI includes
        built-in support for several different kinds of bean, including the
        following Java EE component types:



        managed beans, and EJB session beans. Both managed beans and EJB
        session beans may inject other beans. But some other objects, which
        are not themselves beans in the sense used here, may also have beans
        injected via CDI. In the Java EE platform, the following kinds of
        component may have beans injected:



        message-driven beans,



        interceptors,



        servlets,



        servlet filters and



        servlet event listeners,



        JAX-WS service endpoints and handlers,



        JAX-RS resources,



        providers and javax.ws.rs.core.Application subclasses, and



        JSP tag handlers and tag library event listeners.




        it means you also can use @Inject annotation in your JAX-RS source class.



        there are also some predefined Beans in CDI such as ServletContext that you can use @inject annotation to inject them.



        https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
        https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 20:49









        Mehran Mastcheshmi

        398210




        398210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432978%2fcdi-failure-with-dependent-applicatio-scope-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...