This discussion is archived
2 Replies Latest reply: Jul 22, 2012 9:54 PM by r035198x RSS

java EE6 Jax-RS and CDI managed beans

893622 Newbie
Currently Being Moderated
Im struggling with injecting a bean in a jax-rs endpoint, its crashing everytime.

I need to keep state, so that each time I dont have to reconstruct the whole object/state belonging to a client

Any idea, how to do that?
  • 1. Re: java EE6 Jax-RS and CDI managed beans
    893622 Newbie
    Currently Being Moderated
    My Jax-rs endpoint


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package cgs.service;

    import cgs.db.GamesStatePersistence;
    import cgs.db.MyConnect;
    import cgs.man.CGSBeanMan;
    import cgs.man.CGSBeanManInterface;
    import cgs.man.CGSServices;
    import javax.ejb.EJB;
    import javax.ejb.Stateful;
    import javax.enterprise.context.RequestScoped;

    import javax.enterprise.context.SessionScoped;
    import javax.enterprise.inject.Instance;
    import javax.inject.Inject;
    import javax.ws.rs.*;
    import javax.ws.rs.core.Context;

    import javax.ws.rs.core.UriInfo;
    import javax.inject.Qualifier;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.ResponseBuilder;


    /**
    * REST Web Service
    *
    * @author ameeta
    */
    @Path("gameserv")
    @Stateful
    @RequestScoped
    public class CGSResource {

    private String response;

    @Context
    private UriInfo context;


    /**
    * Creates a new instance of CGSResource
    */
    @EJB
    private CGSBeanMan cgs;


    public CGSResource(){

    }





    // Services cmd= 1.login 2.register 3.regEC 4.bet
    @GET //change to @POST later
    @Produces("application/xml")
    @Path("login")
    public String getXml(@QueryParam("user") String com) {




    return "<a>"+ com+"</a>";
    }



    @GET
    @Produces("application/xml")
    @Path("register")
    public String register(){
    return "<register>asdasdasd</register>";
    }


    @GET
    @Produces("application/xml")
    @Path("bet")
    public String bet(){

    return "<bet>1322222222222</bet>";
    }


    @GET
    @Produces("application/xml")
    @Path("savestate")
    public String savestate(@QueryParam("st") int state){
    int rnd=0;
    cgs.setRound(state);
    return "<state>"+rnd+"</state>";
    }


    }






    ***************************************************

    AND MY BEAN


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package cgs.man;

    import cgs.db.GamesStatePersistence;
    import java.io.Serializable;
    import java.lang.annotation.Retention;
    import javax.annotation.ManagedBean;
    import javax.ejb.Local;
    import javax.ejb.Stateful;
    import javax.ejb.Stateless;
    import javax.enterprise.context.ApplicationScoped;
    import javax.enterprise.context.SessionScoped;
    import javax.inject.Inject;
    import javax.inject.Named;
    import javax.inject.Qualifier;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;



    /**
    *
    * @author ameeta
    */
    @Stateful
    public class CGSBeanMan {   //actual bean holder
    @PersistenceContext
    private EntityManager em;

    private int round;


    public void setRound(int rnd){

    }

    public int getRound(){
    int rnd=1121212;

    return rnd;
    }





    }
  • 2. Re: java EE6 Jax-RS and CDI managed beans
    r035198x Pro
    Currently Being Moderated
    What error message did you get?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points