Bom dia,
Estou criando um projeto com a seguinte estrutura:
Um WS rest (resource) que nele injeto um EJB (service) e o service eu chamado o DAO (EJB).
Porém, o service no rest não está sendo injetado, sempre fica null. O que estou fazendo de errado? rsrs.
Estou usando a seguinte configuração: JBoss 7.1.1 Maven 3.3.9
Uso o maven para gerar o WAR (mvn package)
Segue os códigos principais (alguns imports foram omitidos):
RESOURCE
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/helloWorld")
public class HelloWorldResource {
@Inject
private HelloWorldService service;
@GET
@Path("/findAll")
public Response findAll() {
List<HelloWorld> list = this.service.findAll();
return Response.status(200).entity(list.toString()).build();
}
}
SERVICE
import javax.ejb.Stateless;
import javax.inject.Inject;
@Stateless
public class HelloWorldService {
@Inject
HelloWorldDAO dao;
public List<HelloWorld> findAll() {
return this.dao.findAll();
}
}
DAO
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class HelloWorldDAO {
@PersistenceContext
private EntityManager manager;
@PostConstruct
void afterCreate() {
System.out.println("HelloWorldDAO criado com sucesso");
}
public List<HelloWorld> findAll() {
return this.manager.createQuery("select hw from HelloWorld hw", HelloWorld.class).getResultList();
}
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.helloworld.service</groupId>
<artifactId>helloworld-service</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld-service Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<version>1.0.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<finalName>helloworld-service</finalName>
</build>
</project>