I created a REST application by netbeans, and now I want to upload it to OpenShift.
I'm using eclipselink to do data persistence, so I use a AbstractFacade<T>
class to do the basic CRUD, as follows:
public abstract class AbstractFacade<T> {
private final Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
[...]
}
public int count() {
[...]
}
}
So I inherit this class for the class I want to run the CRUD, for example:
@Stateless
@Path("categoria")
public class CategoriaFacadeREST extends AbstractFacade<Categoria> {
@PersistenceContext(unitName = "api_api_war_1.0PU")
private EntityManager em;
public CategoriaFacadeREST() {
super(Categoria.class);
}
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Categoria entity) {
super.create(entity);
}
@PUT
@Path("{id}")
@Consumes({"application/xml", "application/json"})
public void edit(@PathParam("id") Integer id, Categoria entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Categoria find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public List<Categoria> findAll() {
return super.findAll();
}
[...]
}
As you can see, I use the @Path("categoria")
annotation, since I'm using a REST API and I have an inheritance from the Application class with the @javax.ws.rs.ApplicationPath("/api")
annotation.
But when I try to access meuhost.com/api/categoria
, for example, I get the following error:
HTTP Status 500 - java.lang.NullPointerException
**exception**
javax.servlet.ServletException: java.lang.NullPointerException
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:421)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
**root cause**
java.lang.NullPointerException
com.rhcloud.api.encontreoferta.model.service.AbstractFacade.findAll(AbstractFacade.java:42)
com.rhcloud.api.encontreoferta.model.service.CategoriaFacadeREST.findAll(CategoriaFacadeREST.java:68)
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)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:164)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:203)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
My problem comes down to EJB
.
I really wanted to separate the logic in one server and the interface in another ... And a few more things. So I thought I'd use EJB
.
Because the OpenShift server is Tomcat, Tomcat is just a Servlet Container, that is, it implements the specifications for Servlets and JSP
. Tomcat does not implement EJB
.
As described in link
And that's why my application does not run on OpenShift, I need to use glassfish to meet the requirements of my project ...
In my case the EntityManager
comes null, because it was not possible to properly%% injection.
I would greatly appreciate Bruno César for the great help.