I'm trying to make a restfull endpoint using Jersey to an existing application.
When I try to get a list of data (Mails) from my database I get the following error:
java.lang.NullPointerException
product.security.restcontroller.MailController.allMails(MailController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
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:1542)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
This is my rest controller:
package product.security.restcontroller;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import product.security.model.Mail;
import product.security.service.entityservice.MailService;
@Path("/mails")
public class MailController {
private MailService mailService;
private List<Mail> allMails;
@GET
@Path("/all")
public List<Mail> allMails() throws Exception {
allMails = mailService.findAll();
return allMails;
}
}
And this is my service function
@Override
public List<Mail> findAll() throws Exception {
Query query = em.createQuery("SELECT e FROM Mail AS e");
return (List<Mail>) query.getResultList();
}
Mail service
< bean id="mailService"
class="product.security.service.entityservice.MailServiceImpl">
</bean>
Please note that the application is already running using struts, and the service above works as expected
Update:
Seems like I have a problem with my services : When I called mailService.findAll() inside my controller,using Jeresey, I dont go to findAll() function, and it throws NullPointerException.
The same way, I tried to call findAll() function directly using Jeresy like below :
@GET
@Path("/all")
public List<Mail> findAll() throws Exception {
Query query = em.createQuery("SELECT e FROM Mail AS e");
return (List<Mail>) query.getResultList();
}
Which did not work eather, and I got a NULL pointer Exception at the line which contains em instance. Knowing that Entity Manager is also managed by using services..
I have already separated xml file which manage the services beans, but it seems like Jeresy cannot see it somehow