1
votes

my configuration is: JBoss Wildfly 8.0.0, EJB 3.0, Resteasy 3.0.6, EAR project with web, ejb, api modules.

I have a rest resource annotated with @Path in my web module:

@Path("/boxes")
@Produces(MediaType.APPLICATION_JSON)
@Stateless
public class BoxServiceResource {

@EJB
private IBoxService boxService;

@GET
@Path("/test")
@Produces("text/plain")
public String test() {
    return "test string";
}

@GET
@Path("/list/{date}/{type}")
public List<BoxDTO> getBoxesForDate(@PathParam("date") String date,
                                    @PathParam("type") DateType type) {

    return boxService.getBoxesForDate(new Date(), DateType.DAY);

}

@GET
@Path("/byId/{id}")
public BoxDTO getBoxById(@PathParam("id") Long id) {

    return boxService.getBoxById(5L);

}
}

This is the bean:

@Stateless
public class BoxServiceImpl implements IBoxService {

@Override
public List<BoxDTO> getBoxesForDate(Date date,
                                    DateType type) {

    List<BoxDTO> boxes = new ArrayList<>(4);
    return boxes;
}

@Override
public BoxDTO getBoxById(Long id) {

    BoxDTO box = new BoxDTO();
    return box;

}

}

Additionally I have the Application extending class which has no overriden implementation:

@ApplicationPath("/rest")
public class BoxesRestApplication extends Application {

}

My web.xml contains only a JSF servlet, and nothing besides that. I have the beans.xml files in each module. However, the EJB won't get injected. I have also tried with @Inject with the same result. The REST resource itself gets published and I can call the test method, but when calling the other two methods it fails with NullPointerException. I have already read tons of tutorials and solutions, however, none worked for me. Thank you your help.

1
How is the IBoxService annotated? The IBoxService and BoxServiceImpl is defined in different EJB Module like BoxServiceResource or all are in the same module (IBoxService, BoxServiceImpl and BoxServiceResource)?Reshi
Can you post StackTrace too?Reshi
There are three modules: web, ejb, api. The API contains the Interface(no annotations here, simple Java Interface). Api is packaged as JAR. Tje Ejb module contains the interface implementation (i.e. Stateless bean) - is packaged es EJB. Web is where the resource class resides. I should mention that I also have a JSF bean in WEB module too, where it gets injected without problems.Filip Majernik
I would suggest to annotate the interface with @ Local annotation, if it does not work try annotating the BoxServiceImpl with @ Local, butin my opinion annotating the interface IBoxService with local shoud work. Of course perfrom clean&build and redeploy to be sure ;)Reshi
I had similar project, the difference was that I had interface and bean in one EJB module. If I forgot to annotate the interface like @ Local I got null pointer, because the bean wasnt wired (because container did not know which one) to the target.Reshi

1 Answers

1
votes

Try annotating the IBoxService with @Local annotation. For more information about this annotation see this. The interface should then look:

@Local
public interface IBoxService{
    public List<BoxDTO> getBoxesForDate(Date date, DateType type);
    public BoxDTO getBoxById(Long id);

    // other methods you would need
}