Java Soap Client Example Wsimport
Understanding JAX-WS wsimport with example
What is wsimport ?
Wsimport is a command line tool provided by JAX-WS to generate all the web services artifacts.
Web service artifacts include web service client support code which will take care of including qualified name and url in the client supporting class.
Generated Web service artifacts (java classes) will be used by web service client to access the published web service.
It's provided as part of Java 1.6 and it is available under JDK_PATH/bin folder
When to use wsimport ?
We should use it whenever we get WSDL file from service provider
Web service provider provides a WSDL file after publishing a web service.
Web service provider provides the URL of the WSDL file as below
http://companyX.com/ws/sampleService?wsdl
In this situation any client or consumer can run wsimport command by passing wsdl file to generate the required client supported files which will be used to consume the published service
wsimport command syntax
-
wsimport -keep -p client http: //companyX.com/ws/sampleService?wsdl
wsimport -keep -p client http://companyX.com/ws/sampleService?wsdl
-keep option specifies that it has to keep the generated files.
-p allows us to specify the java package name in which the generated files are to be placed.
We have given the package name as "client" so that it will keep the generated files in client package.
Let's take wsdl file generated in the previous article
Assume this wsdl file is provided by some company and we have to use it to invoke their published service
-
<!--
-
Published by JAX-WS RI (http: //jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-
-->
-
<!--
-
Generated by JAX-WS RI (http: //jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-
-->
-
<definitions xmlns:wsu= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
-
xmlns:wsp= "http://www.w3.org/ns/ws-policy"
-
xmlns:wsp1_2= "http://schemas.xmlsoap.org/ws/2004/09/policy"
-
xmlns:wsam= "http://www.w3.org/2007/05/addressing/metadata"
-
xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/"
-
xmlns:tns= "http://ws.kb.com/" xmlns:xsd= "http://www.w3.org/2001/XMLSchema"
-
xmlns= "http://schemas.xmlsoap.org/wsdl/" targetNamespace= "http://ws.kb.com/"name= "PrimeNumberServiceImplService" >
-
<types/>
-
<message name= "isPrimeNumber" >
-
<part name= "arg0" type= "xsd:int" />
-
</message>
-
<message name= "isPrimeNumberResponse" >
-
<part name= "return" type= "xsd:boolean" />
-
</message>
-
<portType name= "PrimeNumberService" >
-
<operation name= "isPrimeNumber" >
-
<input wsam: Action = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest" message= "tns:isPrimeNumber" />
-
<output wsam: Action = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse"
-
message= "tns:isPrimeNumberResponse" />
-
</operation>
-
</portType>
-
<binding name= "PrimeNumberServiceImplPortBinding" type= "tns:PrimeNumberService" >
-
<soap:binding transport= "http://schemas.xmlsoap.org/soap/http" style= "rpc" />
-
<operation name= "isPrimeNumber" >
-
<soap:operation soapAction= "" />
-
<input>
-
<soap:body use= "literal" namespace= "http://ws.kb.com/" />
-
</input>
-
<output>
-
<soap:body use= "literal" namespace= "http://ws.kb.com/" />
-
</output>
-
</operation>
-
</binding>
-
<service name= "PrimeNumberServiceImplService" >
-
<port name= "PrimeNumberServiceImplPort" binding= "tns:PrimeNumberServiceImplPortBinding" >
-
<soap:address location= "http://localhost:8888/ws/primeNumber" />
-
</port>
-
</service>
-
</definitions>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.kb.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.kb.com/"name="PrimeNumberServiceImplService"> <types/> <message name="isPrimeNumber"> <part name="arg0" type="xsd:int"/> </message> <message name="isPrimeNumberResponse"> <part name="return" type="xsd:boolean"/> </message> <portType name="PrimeNumberService"> <operation name="isPrimeNumber"> <input wsam:Action="http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest" message="tns:isPrimeNumber"/> <output wsam:Action="http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse" message="tns:isPrimeNumberResponse"/> </operation> </portType> <binding name="PrimeNumberServiceImplPortBinding" type="tns:PrimeNumberService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> <operation name="isPrimeNumber"> <soap:operation soapAction=""/> <input> <soap:body use="literal" namespace="http://ws.kb.com/"/> </input> <output> <soap:body use="literal" namespace="http://ws.kb.com/"/> </output> </operation> </binding> <service name="PrimeNumberServiceImplService"> <port name="PrimeNumberServiceImplPort" binding="tns:PrimeNumberServiceImplPortBinding"> <soap:address location="http://localhost:8888/ws/primeNumber"/> </port> </service> </definitions>
Lets see the steps to be followed as a consumer to consume the published web service
Step 1
Run wsimport command by passing wsdl file as below
-
wsimport -keep -p client http: //localhost:8888/ws/primeNumber?wsdl
wsimport -keep -p client http://localhost:8888/ws/primeNumber?wsdl
Output of wsimport command
Now client code is generated under the path from where we issued wsimport command
We can see 2 source files and 2 class files generated in my folder
Generated files are as below
PrimeNumberService.java
-
package com.kb.ws.client ;
-
import javax.jws.WebMethod ;
-
import javax.jws.WebParam ;
-
import javax.jws.WebResult ;
-
import javax.jws.WebService ;
-
import javax.jws.soap.SOAPBinding ;
-
import javax.xml.ws.Action ;
-
/**
-
* This class was generated by the JAX-WS RI.
-
* JAX-WS RI 2.2.9-b130926.1035
-
* Generated source version: 2.2
-
*
-
*/
-
@WebService(name = "PrimeNumberService", targetNamespace = "http://ws.kb.com/" )
-
@SOAPBinding(style = SOAPBinding.Style.RPC )
-
public interface PrimeNumberService {
-
/**
-
*
-
* @param arg0
-
* @return
-
* returns boolean
-
*/
-
@WebMethod
-
@WebResult(partName = "return" )
-
@Action (input = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest", output = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse" )
-
public boolean isPrimeNumber(
-
@WebParam(name = "arg0", partName = "arg0" )
-
int arg0) ;
-
}
package com.kb.ws.client; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.xml.ws.Action; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebService(name = "PrimeNumberService", targetNamespace = "http://ws.kb.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface PrimeNumberService { /** * * @param arg0 * @return * returns boolean */ @WebMethod @WebResult(partName = "return") @Action(input = "http://ws.kb.com/PrimeNumberService/isPrimeNumberRequest", output = "http://ws.kb.com/PrimeNumberService/isPrimeNumberResponse") public boolean isPrimeNumber( @WebParam(name = "arg0", partName = "arg0") int arg0); }
PrimeNumberServiceImplService.java
-
package com.kb.ws.client ;
-
import java.net.MalformedURLException ;
-
import java.net.URL ;
-
import javax.xml.namespace.QName ;
-
import javax.xml.ws.Service ;
-
import javax.xml.ws.WebEndpoint ;
-
import javax.xml.ws.WebServiceClient ;
-
import javax.xml.ws.WebServiceException ;
-
import javax.xml.ws.WebServiceFeature ;
-
/**
-
* This class was generated by the JAX-WS RI.
-
* JAX-WS RI 2.2.9-b130926.1035
-
* Generated source version: 2.2
-
*
-
*/
-
@WebServiceClient(name = "PrimeNumberServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/primeNumber?wsdl" )
-
public class PrimeNumberServiceImplService
-
extends Service
-
{
-
private final static URL PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;
-
private final static WebServiceException PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;
-
private final static QName PRIMENUMBERSERVICEIMPLSERVICE_QNAME = new QName( "http://ws.kb.com/", "PrimeNumberServiceImplService" ) ;
-
static {
-
URL url = null ;
-
WebServiceException e = null ;
-
try {
-
url = new URL ( "http://localhost:8888/ws/primeNumber?wsdl" ) ;
-
} catch ( MalformedURLException ex) {
-
e = new WebServiceException(ex) ;
-
}
-
PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION = url;
-
PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION = e;
-
}
-
public PrimeNumberServiceImplService( ) {
-
super (__getWsdlLocation( ), PRIMENUMBERSERVICEIMPLSERVICE_QNAME) ;
-
}
-
public PrimeNumberServiceImplService(WebServiceFeature... features ) {
-
super (__getWsdlLocation( ), PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features) ;
-
}
-
public PrimeNumberServiceImplService( URL wsdlLocation) {
-
super (wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME) ;
-
}
-
public PrimeNumberServiceImplService( URL wsdlLocation, WebServiceFeature... features ) {
-
super (wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features) ;
-
}
-
public PrimeNumberServiceImplService( URL wsdlLocation, QName serviceName) {
-
super (wsdlLocation, serviceName) ;
-
}
-
public PrimeNumberServiceImplService( URL wsdlLocation, QName serviceName, WebServiceFeature... features ) {
-
super (wsdlLocation, serviceName, features) ;
-
}
-
/**
-
*
-
* @return
-
* returns PrimeNumberService
-
*/
-
@WebEndpoint(name = "PrimeNumberServiceImplPort" )
-
public PrimeNumberService getPrimeNumberServiceImplPort( ) {
-
return super.getPort ( new QName( "http://ws.kb.com/", "PrimeNumberServiceImplPort" ), PrimeNumberService.class ) ;
-
}
-
/**
-
*
-
* @param features
-
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
-
* @return
-
* returns PrimeNumberService
-
*/
-
@WebEndpoint(name = "PrimeNumberServiceImplPort" )
-
public PrimeNumberService getPrimeNumberServiceImplPort(WebServiceFeature... features ) {
-
return super.getPort ( new QName( "http://ws.kb.com/", "PrimeNumberServiceImplPort" ), PrimeNumberService.class, features) ;
-
}
-
private static URL __getWsdlLocation( ) {
-
if (PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION!= null ) {
-
throw PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION;
-
}
-
return PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION;
-
}
-
}
package com.kb.ws.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebServiceClient(name = "PrimeNumberServiceImplService", targetNamespace = "http://ws.kb.com/", wsdlLocation = "http://localhost:8888/ws/primeNumber?wsdl") public class PrimeNumberServiceImplService extends Service { private final static URL PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION; private final static WebServiceException PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION; private final static QName PRIMENUMBERSERVICEIMPLSERVICE_QNAME = new QName("http://ws.kb.com/", "PrimeNumberServiceImplService"); static { URL url = null; WebServiceException e = null; try { url = new URL("http://localhost:8888/ws/primeNumber?wsdl"); } catch (MalformedURLException ex) { e = new WebServiceException(ex); } PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION = url; PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION = e; } public PrimeNumberServiceImplService() { super(__getWsdlLocation(), PRIMENUMBERSERVICEIMPLSERVICE_QNAME); } public PrimeNumberServiceImplService(WebServiceFeature... features) { super(__getWsdlLocation(), PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features); } public PrimeNumberServiceImplService(URL wsdlLocation) { super(wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME); } public PrimeNumberServiceImplService(URL wsdlLocation, WebServiceFeature... features) { super(wsdlLocation, PRIMENUMBERSERVICEIMPLSERVICE_QNAME, features); } public PrimeNumberServiceImplService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public PrimeNumberServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) { super(wsdlLocation, serviceName, features); } /** * * @return * returns PrimeNumberService */ @WebEndpoint(name = "PrimeNumberServiceImplPort") public PrimeNumberService getPrimeNumberServiceImplPort() { return super.getPort(new QName("http://ws.kb.com/", "PrimeNumberServiceImplPort"), PrimeNumberService.class); } /** * * @param features * A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values. * @return * returns PrimeNumberService */ @WebEndpoint(name = "PrimeNumberServiceImplPort") public PrimeNumberService getPrimeNumberServiceImplPort(WebServiceFeature... features) { return super.getPort(new QName("http://ws.kb.com/", "PrimeNumberServiceImplPort"), PrimeNumberService.class, features); } private static URL __getWsdlLocation() { if (PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION!= null) { throw PRIMENUMBERSERVICEIMPLSERVICE_EXCEPTION; } return PRIMENUMBERSERVICEIMPLSERVICE_WSDL_LOCATION; } }
wsimport has generated the interface PrimeNumberService which has the same method as the original method inside PrimeNumberService at server side.
PrimeNumberServiceImplService has a no-argument constructor that will be used to construct the Service object
PrimeNumberServiceImplService has a method getPrimeNumberServiceImplPort () that returns instance of PrimeNumberService on which service methods has to be called.
Step 2
Client supported code is generated, we can write the client to consume the published web service in a more simple code
Create the simple client class and call the web service using above generated files
PrimeNumberClient.java
-
package com.kb.ws.client ;
-
public class PrimeNumberClient {
-
public static void main( String [ ] args) {
-
PrimeNumberServiceImplService service = new PrimeNumberServiceImplService( ) ;
-
PrimeNumberService primeNumberService = service.getPrimeNumberServiceImplPort ( ) ;
-
System.out.println (primeNumberService.isPrimeNumber ( 13 ) ) ;
-
System.out.println (primeNumberService.isPrimeNumber ( 14 ) ) ;
-
}
-
}
package com.kb.ws.client; public class PrimeNumberClient { public static void main(String[] args) { PrimeNumberServiceImplService service = new PrimeNumberServiceImplService(); PrimeNumberService primeNumberService = service.getPrimeNumberServiceImplPort(); System.out.println(primeNumberService.isPrimeNumber(13)); System.out.println(primeNumberService.isPrimeNumber(14)); } }
we can observe that getPrimeNumberServiceImplPort() method of PrimeNumberServiceImplService generated class has encapsulated the logic to get the server port from QName and serviceEndpointInterface and hence client code is very cleaner and simple.
Step 3
Run the client class
Make sure the service is up and running, refer previous article for publishing the service.
Setp 4
Output
About the Author
Source: http://javainsimpleway.com/understanding-jax-ws-wsimport-with-example/
0 Response to "Java Soap Client Example Wsimport"
Post a Comment