JAX-WS 2: creating a client
Blog type:
- JAX-WS
- Java-related
The previous section describes how to create a server based on the JAX-WS API, This section describes how to create a client to call the WebService Service.
Creating a Client is also quite simple, but you need to know a few things:
1. wsdl file path
You need to read the wsdl definition file provided by the server. For example:
Java code
- URL url = new URL ("http: // localhost: 8088/calculate? Wsdl ");
2. QName of the Service to be called
Multiple services may be defined in a wsdl, so you need to specify the Service name to be called. For example:
Java code
- QName qName = new QName ("http: // test /",
- "CalculatorImplService ");
3. Create a Service
With the qname of the wsdl service, you can create the corresponding service object:
Java code
- Service service = Service. create (url, qName );
4. Get the corresponding Port
With the Service, you can obtain a Port in the Service, for example:
Java code
- Calculator port = service. getPort (Calculator. class );
Here are some notes:
-
- Parameter of the getPort () method: This method always requires the serviceEndpointInterface class object to be added to the serviceEndpointInterface class @ WebService annotation.
- The targetNamespace of serviceEndpointInterface must match the namespace of the corresponding portType. The default value is "http: // package name/". If necessary, You need to define the targetNamespace attribute of @ WebService to change it.
- The name of serviceEndpointInterface always needs to match the name of the PortType to be called. The default type is the class name. If necessary, you must specify the @ WebService name attribute to change it.
- The serviceEndpointInterface must have a method definition corresponding to the operation to be called.
- Last call Method
5. Call Method
After obtaining the corresponding information, you can call the desired method, for example:
Java code
- System. out. println (port. add (1, 2 ));