2
I created a REST application through netbeans, and now I want to upload it to Openshift.
I’m using eclipselink to make data persistence, so I use a class AbstractFacade<T>
to make 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() {
[...]
}
}
Then I inherit this class to the class I wish to run 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 annotation @Path("categoria")
, since I am using a REST API and I have a legacy of the Application class with the annotation @javax.ws.rs.ApplicationPath("/api")
.
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 on one server and the interface on another... And a few more things. So I thought I’d use EJB
.
However the Openshift server is Tomcat, but Tomcat is only a Servlet Container, that is, it implements the Servlets specifications and JSP
. Tomcat does not implement EJB
.
As described in http://solucoestecnologia.blogspot.com.br/2013/02/diferencas-entre-ejb-tomcat-e-glassfish.html
And that’s why my application doesn’t run on Openshift, I need to use glassfish to meet the requirements of my project...
In my case the EntityManager
comes null, why it was not possible to do the injection of the EJB
correctly.
I would like to thank the Bruno César for the great help.
Only enough
404
in openshift, local is OK?– Bruno César
Face yes, only I made a common java web application, and openshift looks like it uses Maven... And if I create a Maven web application and try to create REST the same error happens. Maven has some specific dependency to REST?
– Eleazar
You’re using JAX-RS, so there are dependencies. Hence the question that if it works locally, why if it does, then it is not a problem in the project itself, but rather in the deploy in openshift. The "Guy yes" means that is working locally is?
– Bruno César
Rs... "So dude" works local if it’s not Maven, but I was taking a look here and put the
Jersey-3.1.1
and now it’s no longer giving error404
and yes500 - Servlet execution threw an exception
... I believe it is some other dependency, because I do not own and I think I do not need to set anything inweb.xml
... right?– Eleazar
Updates your question with your setup and error (stacktrace) I’m trying to help you.
– Bruno César
Blz... I updated the question with the bug. vlw
– Eleazar
For this error, see if this answer helps you: Abstractmethoderror using Uribuilder on JAX-RS
– Bruno César
No... The problem persists. But now I notice that I am receiving the following message when I compile
[WARNING] The POM for unknown.binary:javax.ws.rs-api-2.0:jar:SNAPSHOT is missing, no dependency information available
That would have something to do with?– Eleazar
Dude I removed some dependencies including the
javax.ws.rs-api-2.0:jar:SNAPSHOT
and left as you showed in the other reply... Now the mistake isStatus 500 - java.lang.NullPointerException
when I try to use the methodfindAll
of my persistence class... I will update the question again... rsrs– Eleazar
For your mistake probably
EntityManager
is null, see if it is being injected correctly, if theCDI
is working, etc.– Bruno César
@Brunocésar face thank you really, but the problem is that Openshift does not meet some requirements of my project... On the Glassfish flat wheel, but if it is Tomcat, nor local wheel... But vlw guy... Thank you very much!!
– Eleazar
Nice that you solved man, but there wheel EJB yes, I remember creating JEE applications there, we used
jboss-as
, I don’t know if they accept other full profile containers.– Bruno César