4

I have a @WebService class that injects an @EJB. The EJB is packaged in a .jar file which is in the same .war file as the web service classes. The @EJB injection always fails, but I can do a JNDI lookup on the EJB. I've tried making the EJB and its interface @Remote, but that didn't matter. Injection still fails and JNDI lookup still works.

I'm using a version 3.0 web.xml. There's no ejb deployment descriptor in the ejb.jar file, but that is not suppose to matter in EJB 3.1.

An I missing something or is this a bug in Glassfish?

Here's my code.

EJB class and interface packaged in a .jar in the .war file:

//@Remote
public interface ReportServiceI {

    public String testAlive();
}

@Stateless
//@Remote (ReportServiceI.class)
public class ReportService implements ReportServiceI {...}

Web service class:

@WebService(
  targetNamespace = "http://www.reps.corp.com/services/reports/ReportService", 
  portName="ReportPort",
  serviceName="ReportService", 
  endpointInterface="com.corp.reps.reports.ws.server.ReportServiceWSI")

public class ReportServiceWS implements ReportServiceWSI {

  public static Logger logger = Logger.getLogger(ReportServiceWS.class);

//  These all fail
//  @EJB
//  @EJB(beanInterface=ReportServiceI.class)
//  @EJB(lookup="java:global/repsreports/ReportService")
  ReportServiceI reportService;

  public String testAlive() {

    //  this works
    try {
      InitialContext context = new InitialContext();
      reportService = (ReportServiceI)context.lookup("java:global/repsreports/ReportService");
    }
    catch (NamingException ex) {
      logger.error(ex);

      return "InitialContext.lookup() failed.";
    }
Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
  • Did you try using `@Inject` (and providing `beans.xml` on your classpath) or annotating the `@WebService` bean as `@Stateless` (making it an EJB)? – Piotr Nowicki Feb 24 '12 at 07:45
  • No to both. beans.xml is a CDI file and I'm using Glassfish as the injection container. I'll try making the code@WebService bean code@stateless - it's about the only thing I haven't tried - but that shouldn't be necessary. – Dean Schulze Feb 24 '12 at 13:39
  • 1. If you want to test `@Inject`, you must use `beans.xml`. That's right - I wanted to test if CDI would properly inject your bean into WebService. 2. Please read the EJB specification and application server documentation to be sure that you are able to inject / use `@EJB` in Web Services which are not EJB's. – Piotr Nowicki Feb 24 '12 at 13:53
  • possible duplicate of [Glassfish 4.1 can't run RestFul service when using ear/ejb/web module](http://stackoverflow.com/questions/25879898/glassfish-4-1-cant-run-restful-service-when-using-ear-ejb-web-module) – Marvin Emil Brach Mar 08 '15 at 01:47

2 Answers2

1

This is a bug in Glassfish (apparently in the web services stack).

Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
-1

Piotr,

No to both.

beans.xml is a CDI file and I'm using Glassfish as the injection container. I'll try making the @WebService bean @stateless - it's about the only thing I haven't tried - but that shouldn't be necessary.

(This forum software won't allow me to add a comment below Piotr's.)

Dean Schulze
  • 9,633
  • 24
  • 100
  • 165
  • This is not a forum software. You should not post an answer to my comment as an "Answer" as it introduces unnecessary mess. I'd suggest deleting this "Answer" and continue our talk in comments to your question. – Piotr Nowicki Feb 24 '12 at 13:51
  • The comments couldn't take annotations. I tried the `code` meta tags that the help suggests but those didn't work either. – Dean Schulze Feb 24 '12 at 16:15
  • If you use the accent key (the one with the tilde) around the text it's treated as code, so you can put `@Inject` `@EJB` or whatever you want. – Piotr Nowicki Feb 26 '12 at 22:32