I'm using Grizzly instead of Glassfish for this JAX-RS application. I'm new to this technology so I've been googling around a lot and can't seem to find a good outline for how to setup a Service/DAO layer in my applicatiion.
Below is the working prototype that I have.
My Resource
@Path("/helloworld")
@Stateless
public class MyResource {
@EJB //DOESN'T WORK - how do I map this service to this resource?
WorkflowService workflowService;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String TestRequest() {
Workflow workflow = new Workflow();
workflow.setName("test");
workflowService.save(workflow);
return "Workflow ID:";
}
}
My Dao
public class WorkflowDao {
@PersistenceContext(unitName = "unit")
private EntityManager entityManager;
public int save(Workflow workflow) {
entityManager.persist(workflow);
return workflow.getId();
}
}
My Service
@Stateless
public class WorkflowService {
@EJB //I know EJB is probably wrong here, not sure what else to do yet.
WorkflowDao workflowDao;
public int save(Workflow workflow) {
int id = workflowDao.save(workflow);
return id;
}
}
Update - I realize EJB won't work with my setup. So my question is, what does? How do I make the service accessible in my resource?
-------------- Final/Working Code --------------
Resource
@Path("/helloworld")
public class MyResource {
WorkflowService workflowService;
public MyResource() {
workflowService = new WorkflowService();
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String TestRequest() {
Workflow workflow = new Workflow();
workflow.setName("test");
workflowService.save(workflow);
return "Workflow ID:";
}
}
Service
public class WorkflowService {
WorkflowDao workflowDao;
public WorkflowService() {
workflowDao = new WorkflowDao();
}
public int save(Workflow workflow) {
int id = workflowDao.save(workflow);
return id;
}
}
DAO
@Singleton
public class WorkflowDao {
private EntityManager entityManager;
public int save(Workflow workflow) {
getEntityManager().persist(workflow);
return workflow.getId();
}
protected EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("unit");
EntityManager em = emf.createEntityManager();
return em;
}
}