To create a client that invokes a Web service, a Service Endpoint Interface and a service interface are required. These required files can be created from WSDL.
After the required files are prepared, the Dependency Injection can be defined and so on, objects can be associated with the interfaces, and methods can be invoked via the interfaces.
Creating the required Files
The Service Endpoint Interface and so on can be created by the wizard. Select [Web Services] > [Web Service(JAX-WS)] from the [New] wizard.
For details on the creation method, refer to "5.3.5 Creating a Service Endpoint Interface from WSDL".
Getting the Object to the Interface
Objects can be obtained easily by means of a Dependency Injection.
To invoke a Web service, use the @WebServiceRef annotation as shown below.
Example of using the WebServiceRef Annotation
@WebServiceRef(name="service/Calc") private CalcService service; |
Coding Example | Explanation |
---|---|
service/Calc | Specify the name associated with the service. |
CalcService | This is the service interface. |
Note
The Dependency Injection can be used only in Java EE components managed in Java EE containers, such as servlets, EJB, and so on.
For other classes, use JNDI lookup to get objects.
For details, refer to "10.2.2 Using JNDI Lookup to Obtain Objects".
Invoking the Web Service
To invoke the Web service, get the object to the Service Endpoint Interface as shown below, then invoke the method via the interface.
Example of Invoking the Web Service
Calc port = service.getCalcPort(); int result = port.add(100,200); |
Explanation Item | Explanation |
---|---|
Calc | This is the Service Endpoint Interface. |
service | The object fetched by the Dependency Injection or JNDI lookup is set in the service interface field. |
port.add(100,200) | The method published by the Service Endpoint Interface is invoked. The add method is published by the Calc interface. |