Search code examples
javarestglassfishejb

Restful with ejb


I want to create Restful application with ejb, so I create db.jar and restful.war.

In db.jar I have:

@Remote public interface DBManagerRemote

And in restful.war I have:

 @EJB private DBManagerRemote manager;

But when I deploy in glassfish I get an exception:

org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException

What am I doing wrong?

web.xml:

<web-app 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"
         version="3.0">
    <display-name>Restful Web Application</display-name>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

persistence.xml in db.jar:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" 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">

    <persistence-unit name="simpleFactory" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>source_name</jta-data-source>
        <class>Mybean</class>
    </persistence-unit>
</persistence>

Solution

  • You are getting a NullPointerException because you're attempting to use container injection in a REST application that is using RestEasy in GlassFish.

    RestEasy is not integrated with GlassFish, so injection will never occur.

    The easiest solution for you is to:

    1. remove any resteasy jars that you have in your application
    2. completely remove the web.xml
    3. add some standard JAX-RS bootstrap code to your web app:

      @ApplicationPath("/")
      public class JAXRSConfiguration extends Application {
      }
      

    This will then use the JAX-RS implementation that is built into GlassFish.