0
votes

I have a project maven - EAR, in the same i have a RESTful JAX-RS that work with jersey and it is consuming with JSON, when I try get list to RESTful since EJB, bring not nothing. However when add list in the same REST it works well.!

this is the RESTful with list in the same class!.

@Path("/courseMockRS")
@Consumes({"application/json"})
@Produces({"application/json"})
public class CourseMock_RS {

@GET
@Path("/all_courses")
public List<CourseMock> AllCourses() {
    List<CourseMock> courses = getCoursesMock();
    return courses;
}

public List<CourseMock> getCoursesMock() {
    List<CourseMock> coursesMocks = new ArrayList<CourseMock>();

    CourseMock mock = new CourseMock();
    mock.setName("SOA Analist");
    mock.setAuthor("SOA Education");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    mock = new CourseMock();
    mock.setName("SOA Architect");
    mock.setAuthor("SOA ET");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    mock = new CourseMock();
    mock.setName("SOA Consultant");
    mock.setAuthor("SOA ET");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    return coursesMocks;
}
}

Thus it works well!

[{"name":"SOA Analist","author":"SOA Education","releaseDate":"16/07/2015"},    
{"name":"SOA Architect","author":"SOA ET","releaseDate":"16/07/2015"},
{"name":"SOA Consultant","author":"SOA ET","releaseDate":"16/07/2015"}]

But when implement EJB not found!, However the application it is deployed and the Stateless resource is accepted.

This is the resources from EJB.

DTO

public class CourseMock {

private String name;
private String author;
private String releaseDate;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(String releaseDate) {
    this.releaseDate = releaseDate;
}   
}

Facade

@Stateless
@LocalBean
public class CourseFacadeMock implements ICourseMockFacade{

private CourseMock mock;

@Override
public List<CourseMock> getCoursesMock() {
    List <CourseMock> coursesMocks = new ArrayList<CourseMock>();

    mock= new CourseMock();
    mock.setName("Analista SOA");
    mock.setAuthor("SOA Education");
    mock.setReleaseDate("16/07/2015");        
    coursesMocks.add(mock);

    mock= new CourseMock();
    mock.setName("SOA Analist");
    mock.setAuthor("SOA Education");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    mock = new CourseMock();
    mock.setName("SOA Architect");
    mock.setAuthor("SOA ET");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    mock = new CourseMock();
    mock.setName("SOA Consultant");
    mock.setAuthor("SOA ET");
    mock.setReleaseDate("16/07/2015");
    coursesMocks.add(mock);

    return coursesMocks;

}

}

Interface Facade

@Local
public interface ICourseMockFacade {

public List<CourseMock> getCoursesMock();
}

And the RESTful

@Path("/courseMockRS")
@Consumes({"application/json"})
@Produces({"application/json"})
public class CourseMock_RS {

@EJB
ICourseMockFacade courseMockFacade;

@GET
@Path("/all_courses")
public List<CourseMock> AllCourses() {
    List<CourseMock> courses = courseMockFacade.getCoursesMock();
    return courses;
}    
}

Thus fails.. The Exception is NUllPointerException. Help, please!.

2
Could you post the stack trace of your NullPointerException? Thanks.hugh
Ok, this in answer. Thanks.BASP
Thanks. For future reference, it would be better to edit the question to include the stack trace - it isn't an answer, so posting it as one will annoy the admins.hugh

2 Answers

1
votes

I´ve created a project with your code and it's working perfectly assuming you have in place the JAX-RS Configuration class with something like:

@ApplicationPath("resources")
public class JAXRSConfiguration extends Application {

}

In this case, if the EJB is not injected, maybe, the problem is in your beans.xml file, the property bean-discovery-mode should be set to "all". As an example:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all">
</beans>

I hope it helps.

0
votes

Ok, that looks like courseMockFacade isn't getting injected into CourseMock_RS. The reason is that the @EJB annotation is intended for use in EJBs, you can't put it into arbitrary classes list JAXRS interfaces.

To get hold of an ICourseMockFacade object you can use a JNDI lookup, something like:

ICourseMockFacade courseMockFacade=lookupCourseFacade();

private ICourseMockFacade lookupCourseFacade(){
    return (ICourseMockFacade)new InitialContext().lookup("java:module/CourseMockFacade");
}

With some caveats: I'm assuming here that you have an EJB CourseMockFacade which implements ICourseMockFacade, and the JNDI address will depend on your deployment - full details here.