18

From the client side, if I want to access a web service, I would simply generate a proxy for it using wsimport. That is my web service reference.

Where then does the annotation @WebServiceRef come into play? Is it meant to be used at the server side only, to obtain injected references to web services hosted in the same environment?

Jops
  • 22,535
  • 13
  • 46
  • 63
  • In java SE clients it is not needed as it seems: `//@WebServiceRef(wsdlLocation="http://localhost:8080/JaxWsExample/NewWebServiceService?wsdl") private static NewWebServiceService service = new NewWebServiceService();` and it works – ACV Oct 26 '15 at 09:46
  • However in servlet based clients, it is mandaory (and no need to instantiate the service) – ACV Oct 26 '15 at 09:51

2 Answers2

21

Not necessarily, but it really is something that depends on the server implementation. e.g. To access a remote service, it requires to have access to generated client and optionally to the WSDL documents and schemes files (by convention should be in WEB-INF/wsdl), so that

public class HelloServlet extends HttpServlet {

    @WebServiceRef(HelloMessengerService.class) // class with @WebServiceClient
    private HelloMessenger port; // the SEI

    ...
}

The HelloMessengerService class is the stub and has the @WebServiceClient annotation which has a wsdlLocation attribute (points to local o remote WSDL document).

But you can have something like that

@WebServiceRef(wsdlLocation = "META-INF/wsdl/AnyService/Any.wsdl")
private HelloMessengerService service;

or

@WebServiceRef
public HelloMessengerService service;

If you use a handler chain to alter incoming and outgoing SOAP messages:

@WebServiceRef(HelloMessengerService.class)
@HandlerChain(file="handler-chain.xml")
private HelloMessenger port;

The use of the @WebServiceRef annotation must be applied to JAX-WS-managed clients, like a Servlet, EJB, or another Web service.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • 1
    The differences between Java EE components and "standard" Java classes are that Java EE components are assembled into a Java EE application, they are verified to be well formed and in compliance with the Java EE specification, and they are deployed to production, _where they are run and managed by the Java EE server_. [more...](http://docs.oracle.com/javaee/6/tutorial/doc/bnaay.html#bnabb) – Paul Vargas Mar 30 '13 at 12:51
5

Just to add to Paul Vargas' answer, the @WebServiceRef annotation is a tool to complete the evolution of the Java EE platform to a wholly managed environment. Think about it this way:

Almost every component within the Java EE stack is injectable by some means, EJBs, JSF managed beans, CDI beans, @Resources etc. Why not be able to inject a webservice reference? With the ability to inject a webservice reference using this annotation, the following are ready injection targets:

  • EJBs
  • Servlets (under Servlet 3.0)
  • JSF Managed Beans
  • CDI Beans
  • MDBs
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
kolossus
  • 20,559
  • 3
  • 52
  • 104