2

In my Web service, I have:

@WebService(serviceName = "myservice")
public class ServiceName{

    @Resource
    private WebServiceContext context;

In a stateless class I want to use the same operation:

@Stateless
public class MakeHappen{

 @Resource
        private WebServiceContext context;

But I receive an EJB exception. How can I inject this resource, outside webservice?

Goldbones
  • 1,407
  • 3
  • 21
  • 55

3 Answers3

1

AFAIK no, only in the context of a Webservice.

See Interface WebServiceContext

A WebServiceContext makes it possible for a Webservice endpoint implementation class to access message context and security information relative to a request being served. Typically a WebServiceContext is injected into an endpoint implementation class using the @Resource annotation.

In your case, you should to decorate stateless EJB as Webservice. Open methods of the stateless EJB can be represented as Webservices.

@Stateless
@WebService
public class MakeHappen {

@Resource
private WebServiceContext context;

...
  • There is a similar object to webservicecontext that can be used on ejb container? – Goldbones May 08 '15 at 12:57
  • Regarding, to my last question -http://stackoverflow.com/questions/30105806/add-path-parameters-to-response-fields/30106595#30106595, it´s possible to use some context to determine my request path and its parameters? – Goldbones May 08 '15 at 13:34
  • Yes, I understand differences between containers. It´s not my question. How can I get SOAP content using a filter - My main question question is still the same. How can I add a header dynamically? – Goldbones May 08 '15 at 13:47
1

Web service endpoint belongs to a web tier and is managed by a web container (See Java EE 7 tutorial 6.1 Web Applications for more info).

Enterprise bean on the other hand is a business logic component. It is managed by EJB container (Java EE 7 tutorial 32.1 What Is an Enterprise Bean?).

This means that enterprise bean could not have WebServiceContext injected as it is managed by different container. This would also make no sense as EJB container have no WebServiceContext.

Aleksandr Erokhin
  • 1,904
  • 3
  • 17
  • 32
1

I had the same problem and this is how I solved it:

WebServiceContext wscontext = null;
try {
    Context ctx = new InitialContext();
    wscontext = (WebServiceContext) ctx.lookup("java:comp/WebServiceContext");
} catch (NamingException e) {

}