Olá!!!
Eu estou pesquisando há tempos como eu posso realizar a integração do JAX-RS, CDI, JTA no Wildfly.
Os testes funcionam para o CRUD no banco de dados. Mas como eu faco a injecao de daos e suas respectivas entityManagers?
Seguem os codigos:
UserResource.java
package com.alurajaxrs.resource;
import java.net.URI;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.alurajaxrs.dao.UserDao;
import com.alurajaxrs.model.User;
import com.thoughtworks.xstream.XStream;
@Path("user")
public class UserResorce {
@Inject
private UserDao daoUser;
@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
public String search(@PathParam("id") int id){
User user = daoUser.searchById(id);
return user.toXML();
}
@POST
@Consumes(MediaType.APPLICATION_ATOM_XML)
public Response add(String content){
User user = (User) new XStream().fromXML(content);
daoUser.insert(user);
return Response.created(URI.create("/user/" + user.getIdUser())).build();
}
}
UserDao.java
package com.alurajaxrs.dao;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.alurajaxrs.model.User;
import com.alurajaxrs.util.GlobalVariables;
@SuppressWarnings("serial")
@Stateless //EJB
public class UserDao implements Serializable{
@PersistenceContext(unitName=GlobalVariables.DATABASE)
EntityManager em;
private DAO<User> dao;
@PostConstruct
void init() {
this.dao = new DAO<User>(this.em, User.class);
}
public void constructDao(EntityManager em){
this.em = em;
this.dao = new DAO<User>(this.em, User.class);
}
public void insert(User o){
dao.insert(o);
}
public void update(User o){
dao.update(o);
}
public void remove(User o){
this.em.remove(o);
}
public User searchById(Integer ido){
return dao.searchById(ido);
}
public List<User> listAll(){
return dao.listAll();
}
public int countAll() {
return dao.countAll();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>alurajaxrs-webserver</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.alurajaxrs.resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="dbname" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/dbame</jta-data-source>
<class>com.alurajaxrs.model.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</properties>
</persistence-unit>
O servidor que eu uso é o wildfly 8.2.1 e ele está subindo e eu consigo acessar o index.html por http://localhost:8080/alurajaxrs-webserver/.
Porém, quando eu tento acessar: http://localhost:8080/alurajaxrs-webserver/webapi/user/78, no browser aparece: "Not Found" e no O console do wildfly não dá nenhum erro
Pleaseeee helppppp!!!