Search code examples
restdependency-injectionjax-rsejbwebsphere

Interface EJB bean is not injected in JAX-RS Implementation class?


I am using WAS 8.5 , RAD 9.0 , JAX-RS 1.1 , Java EE 1.6, EJB 3.0 . There is WebModule(contains rest classes), EJB Module(contain all EJB Implementation classes). Implementing Rest Webservice using jax-rs , and getting NullPointerExpection while PUT Service call.

Below is my code snippet:

Servlet web.xml in Web Module which refers JAX-RS Class in EJB Module.(com.implement.services.RestServiceInvoke)

<servlet>
        <description>JAX-RS Tools Generated - Do not modify</description>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
        <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>com.implement.services.RestServiceInvoke1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <enabled>true</enabled>
        <async-supported>false</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <url-pattern>/jaxrs/*</url-pattern>
    </servlet-mapping>

JAX-RS Class like

Note: No @ApplicationPath() Annotaion Used.

public class RestServiceInvoke1 extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(ImplementationRest.class);
        return classes;
    }
}

Interface Class like:

Note : @Local annotation Included in interface class thats also not working

public interface IStudentServices {
 public String getValue(String value);
}

@Stateless
public class StudentServiceBean implements IStudentServices {
   @Override
   public String getValue(String value){
        return value;
   }
}

and Service Implementation Class like:

@Stateless
@Path("partyservices")
@Resource(name="jdbc/dbname", type=DataSource.class)
public class ImplementationRest implements IEmployeeServices {
      @EJB
      private IStudentServices iStudentSerives;

      @PUT
      @Path("/setprimary")
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      @Override
      public String getValues1(SamplePojo person){
        iStudentSerives.getValue("string");
        return value;
      }
}

iStudnetServices.getValue("string") gets NullpointerException. I mean IStudentServices is not injected from container. Already gone through similar error over web , but no solutions. Kindly help me to resolve this issue. Thanks in advance.


Solution

  • ImplementationRest EJB bean is instaniated using javax.ws.rs.core.Application instead EJB Container.

    public class RestServiceInvoke1 extends Application {
    
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(ImplementationRest.class);
        return classes;
    }}
    

    This is the reason EJB Bean classes not injected in to anohter Bean that instantiated via javax.ws.rs.core.Application. Afterwards i achieved this implementation by Spring Framework like ejb-local-ref in web.xml , jee:jndi-lookup in bean.xml. Please go through with IBM Support URLs.

    Go through below link for Summarizing this issue.

    http://wedowebsphere.de/blogpost/accessing-jpa-jax-rs-service-directly