Persistence context injected in Restful web service jersey on Jboss is always null
pcg4m3s Oct 11, 2013 5:36 AMI'm implementing a simple web service using Jersey.
I must to save a person (calling a restful resource) on my database postgres connected to jboss datasource.
When I call the restful resource, the entity manager is always null.
This is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JerseyRest" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
<class>mypackage.persistenza.Persona</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
This is my class entity:
package mypackage.persistenza;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Entity
@NamedQueries ({
@NamedQuery(name = "Persona.findPersoneByCognome", query = "SELECT p FROM Persona p WHERE p.cognome = :cognome"),
@NamedQuery(name = "Persona.getPersone", query = "SELECT p FROM Persona p"),
@NamedQuery(name = "Persona.findPersoneByCognomeNome", query = "SELECT p FROM Persona p WHERE p.cognome = :cognome AND p.nome = :nome")
})
public class Persona implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
Long id;
String nome;
String cognome;
public Persona() {
}
public Persona(String nome, String cognome) {
super();
this.nome = nome;
this.cognome = cognome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCognome() {
return cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
public Long getId() {
return id;
}
@Override
public String toString() {
return "Persona [id=" + id + ", nome=" + nome + ", cognome=" + cognome + "]";
}
}
There are classes used for restful:
package mypackage.restful.jersey;
import mypackage.persistenza.Persona;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
@LocalBean
public class PersonaRestDAO {
@PersistenceContext
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void aggiungiPersona(Persona p)
{
em.persist(p);
}
}
--------------------
package mypackage.restful.jersey;
import mypackage.persistenza.Persona;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Stateless
@LocalBean
@Path("/persona")
public class PersonaRestWS {
@EJB PersonaRestDAO personaDAO;
// http://localhost:8080/JerseyRest/rest/persona/nuovaPersonaGetParam/nome/cognome
@GET
@Path("/nuovaPersonaGetParam/{nome}/{cognome}")
public Response nuovaPersonaGetParam(@PathParam("nome") String nome, @PathParam("cognome") String cognome) {
personaDAO.aggiungiPersona(new Persona(nome, cognome));
return Response.status(200).entity("elemento inserito").build();
}
}
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>MyJersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>mypackage.restful.jersey.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<servlet-mapping>
<servlet-name>MyJersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<persistence-context-ref>
<persistence-context-ref-name>mypackage.restful.jersey.PersonaRestDAO/em</persistence-context-ref-name>
<persistence-unit-name>JerseyRest</persistence-unit-name>
</persistence-context-ref>
</web-app>
And this is my pom.xml in my maven project:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroupid</groupId>
<artifactId>JerseyRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- usato per persistenza -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0-RC1</version>
</dependency>
<!-- fine usato per persistenza -->
<!-- usato per annotation stateless -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
<!-- fine usato per annotation stateless -->
<!-- usato per annotation localbean -->
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>ejb31-api-experimental</artifactId>
<version>3.1.1</version>
</dependency>
<!-- fine usato per annotation localbean -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is my error message:
java.lang.NullPointerException
mypackage.restful.jersey.PersonaRestWS.nuovaPersonaGetParam(PersonaRestWS.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)