Hi,
Based on the query of one of Our Magic Subscriber “Mauricio” we are going to see how we can create a JAXWS WebService using Contract First (from WSDL) mechanism. So basically in Top to Bottom approach (Contract First Approach). We already have an existing WSDL and based on that we want to create a WebService. WebLogic provides a best utility to achieve the same. The utility is called as “weblogic.wsee.tools.anttasks.WsdlcTask” using this utility we can create any kind of WebService from a WSDL just by specifying type=”JAXWS” or type=”JAXRPC” as an attribute to the above ANT task.
Suppose we already have a contract (WSDL) like following “ComplexImpl.wsdl” which internally can contain some Complex Data Types as well… The Following WSDL contains a Complex Datatype as “BasicStruct”
==============>
Step1). Create a directory somewhere in your file system like : “/demos/TopDown” and then place the above WSDL inside this directory.
<?xml version="1.0" encoding="UTF-8"?> <s0:definitions name="ComplexServiceDefinitions" targetNamespace="http://example.org" xmlns="" xmlns:s0="http://schemas.xmlsoap.org/wsdl/" xmlns:s1="http://example.org" xmlns:s2="http://schemas.xmlsoap.org/wsdl/soap/"> <s0:types> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://example.org" xmlns:s0="http://schemas.xmlsoap.org/wsdl/" xmlns:s1="http://example.org" xmlns:s2="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:import namespace="java:examples.webservices.complex"/> <xs:element name="echoComplexType"> <xs:complexType> <xs:sequence> <xs:element name="struct" type="java:BasicStruct" xmlns:java="java:examples.webservices.complex"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="echoComplexTypeResponse"> <xs:complexType> <xs:sequence> <xs:element name="EchoStructReturnMessage" type="java:BasicStruct" xmlns:java="java:examples.webservices.complex"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="echoInt"> <xs:complexType> <xs:sequence> <xs:element name="IntegerInput" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="echoIntResponse"> <xs:complexType> <xs:sequence> <xs:element name="IntegerOutput" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="java:examples.webservices.complex" xmlns:s0="http://schemas.xmlsoap.org/wsdl/" xmlns:s1="http://example.org" xmlns:s2="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="BasicStruct"> <xs:sequence> <xs:element minOccurs="1" name="IntValue" nillable="false" type="xs:int"/> <xs:element minOccurs="1" name="StringValue" nillable="true" type="xs:string"/> <xs:element maxOccurs="unbounded" minOccurs="0" name="StringArray" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </s0:types> <s0:message name="echoComplexType"> <s0:part element="s1:echoComplexType" name="parameters"/> </s0:message> <s0:message name="echoComplexTypeResponse"> <s0:part element="s1:echoComplexTypeResponse" name="parameters"/> </s0:message> <s0:message name="echoInt"> <s0:part element="s1:echoInt" name="parameters"/> </s0:message> <s0:message name="echoIntResponse"> <s0:part element="s1:echoIntResponse" name="parameters"/> </s0:message> <s0:portType name="ComplexPortType"> <s0:operation name="echoComplexType" parameterOrder="parameters"> <s0:input message="s1:echoComplexType"/> <s0:output message="s1:echoComplexTypeResponse"/> </s0:operation> <s0:operation name="echoInt" parameterOrder="parameters"> <s0:input message="s1:echoInt"/> <s0:output message="s1:echoIntResponse"/> </s0:operation> </s0:portType> <s0:binding name="ComplexServiceSoapBinding" type="s1:ComplexPortType"> <s2:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <s0:operation name="echoComplexType"> <s2:operation style="document"/> <s0:input> <s2:body parts="parameters" use="literal"/> </s0:input> <s0:output> <s2:body parts="parameters" use="literal"/> </s0:output> </s0:operation> <s0:operation name="echoInt"> <s2:operation style="document"/> <s0:input> <s2:body parts="parameters" use="literal"/> </s0:input> <s0:output> <s2:body parts="parameters" use="literal"/> </s0:output> </s0:operation> </s0:binding> <s0:service name="ComplexService"> <s0:port binding="s1:ComplexServiceSoapBinding" name="ComplexPortTypeSoapPort"> <s2:address location="http://localhost:7001/ComplexImpl/ComplexImpl"/> </s0:port> </s0:service> </s0:definitions>
Step2). Now write the following kind of ANT build script to use the WSDLC utility to create a dummy WebService with default implementation of Service Operations (Methods)
“build.xml”
<project name="GenerateWS" default="all"> <property name="build" value="output" /> <property name="adminurl" value="t3://localhost:7001" /> <property name="serverName" value="AdminServer" /> <property name="userName" value="weblogic" /> <property name="password" value="weblogic" /> <property name="ear.name" value="Complex_JAXWS_Ear" /> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="wsdlc" classname="weblogic.wsee.tools.anttasks.WsdlcTask"/> <target name="all" depends="generate-from-wsdl,wait"/> <target name="generate-from-wsdl"> <wsdlc srcWsdl="ComplexImpl.wsdl" destJwsDir="jarJws" destImplDir="." type="JAXWS" packageName="ws"/> </target> <target name="wait"> <echo message=" Now you need to provide your own Implementation for sayHello() of [[[ ws/ComplexService_ComplexPortTypeSoapPortImpl.java ]]]" /> <echo message=" then you need to run 'ant deploy' to rebuild your edited Service and to Deploy it on the Server..." /> </target> <target name="build-service"> <mkdir dir="${build}/${ear.name}" /> <jwsc srcdir="." destdir="${build}/${ear.name}"> <jws file="ws/ComplexService_ComplexPortTypeSoapPortImpl.java" compiledWsdl="jarJws/ComplexImpl_wsdl.jar" type="JAXWS"/> </jwsc> </target> <target name="deploy" depends="build-service"> <wldeploy action="deploy" source="${build}/${ear.name}" user="${userName}" password="${password}" verbose="true" failonerror="yes" adminurl="${adminurl}" targets="${serverName}" /> </target> <target name="redeploy"> <wldeploy action="redeploy" source="${build}/${ear.name}" user="${userName}" password="${password}" verbose="true" failonerror="yes" adminurl="${adminurl}" targets="${serverName}" /> </target> </project>
Step3). Now Open a command prompt then run the “. ./setWLSEnv.sh” to set the CLASSPATH and PATH properly.
Run the “setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
. ./setWLSEnv.sh
Note: the first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.
Step4). Now as soon as you will run the command like “ant -buildfile build.xml generate-from-wsdl” it will generate all the artifacts for your WebService and a Dummy WebService implementation.
ant -buildfile build.xml Buildfile: build.xml generate-from-wsdl: [wsdlc] Download file [ComplexImpl.wsdl] to /tmp/_ckr59b [wsdlc] srcWsdl is redefined as [ /tmp/_ckr59b/ComplexImpl.wsdl ] [wsdlc] Consider using <depends>/<produces> so that wsimport won't do unnecessary compilation [wsdlc] parsing WSDL... [wsdlc] [WARNING] src-resolve: Cannot resolve the name 'java:BasicStruct' to a(n) 'type definition' component. [wsdlc] line 9 of file:/tmp/_ckr59b/ComplexImpl.wsdl#types?schema1 [wsdlc] generating code... [wsdlc] compiling code... [wsdlc] Note: /demos/TopDown/jarJws/generated/ws/ComplexService.java uses unchecked or unsafe operations. [wsdlc] Note: Recompile with -Xlint:unchecked for details. [AntUtil.deleteDir] Deleting directory /tmp/_ckr59b wait: [echo] Now you need to provide your own Implementation for sayHello() of [[[ ws/ComplexService_ComplexPortTypeSoapPortImpl.java ]]] [echo] then you need to run 'ant deploy' to rebuild your edited Service and to Deploy it on the Server... all: BUILD SUCCESSFUL Total time: 3 seconds
Step5). Inside “/demos/TopDown/ws” you will find a WebService implementation class is generated with some name like “ComplexPortTypeSoapPortImpl.java” this is actually our WebService like following:
NOTE: A JAR also will be generated inside “/demos/TopDown/jarJws/ComplexImpl_wsdl.jar” this JAR will basically contain all the required classes and the Complex Datatypes which is going to be used by the WebService.
package ws; import javax.jws.WebService; import javax.xml.ws.BindingType; /** * This class was generated by the JAX-WS RI. * Oracle JAX-WS 2.1.3-06/19/2008 07:03 PM(bt) * Generated source version: 2.1 * */ @WebService(portName = "ComplexPortTypeSoapPort", serviceName = "ComplexService", targetNamespace = "http://example.org", wsdlLocation = "/wsdls/ComplexImpl.wsdl", endpointInterface = "ws.ComplexPortType") @BindingType("http://schemas.xmlsoap.org/wsdl/soap/http") public class ComplexService_ComplexPortTypeSoapPortImpl implements ComplexPortType { public ComplexService_ComplexPortTypeSoapPortImpl() { } /** * * @param struct * @return * returns ws.BasicStruct */ public BasicStruct echoComplexType(BasicStruct struct) { //replace with your impl here return null; } /** * * @param integerInput * @return * returns int */ public int echoInt(int integerInput) { //replace with your impl here return 0; } }
Step6).Now We need to just add our Business implementation according to our requirement inside the methods present inside the WebService “ComplexPortTypeSoapPortImpl.java” like following:
package ws; import javax.jws.WebService; import javax.xml.ws.BindingType; /** * This class was generated by the JAX-WS RI. * Oracle JAX-WS 2.1.3-06/19/2008 07:03 PM(bt) * Generated source version: 2.1 * */ @WebService(portName = "ComplexPortTypeSoapPort", serviceName = "ComplexService", targetNamespace = "http://example.org", wsdlLocation = "/wsdls/ComplexImpl.wsdl", endpointInterface = "ws.ComplexPortType") @BindingType("http://schemas.xmlsoap.org/wsdl/soap/http") public class ComplexService_ComplexPortTypeSoapPortImpl implements ComplexPortType { public ComplexService_ComplexPortTypeSoapPortImpl() { System.out.println("nt ComplexPortTypeSoapPortImpl Instentiated..."); } /** * * @param struct * @return * returns ws.BasicStruct */ public BasicStruct echoComplexType(BasicStruct struct) { //replace with your impl here System.out.println("nt ComplexPortTypeSoapPortImpl echoComplexType method called...Got struct = "+struct); return struct; } /** * * @param integerInput * @return * returns int */ public int echoInt(int integerInput) { System.out.println("nt ComplexPortTypeSoapPortImpl echoInt method called...Got integerInput = "+integerInput); return integerInput; } }
Step7). Now just run the ANT build again to build and deploy the WebService as following “ant -buildfile build.xml deploy”
ant deploy Buildfile: build.xml build-service: [mkdir] Created dir: /demos/TopDown/output/Complex_JAXWS_Ear [jwsc] JWS: processing module /ws/ComplexService_ComplexPortTypeSoapPortImpl [jwsc] Parsing source files [jwsc] Parsing source files [jwsc] 1 JWS files being processed for module /ws/ComplexService_ComplexPortTypeSoapPortImpl [jwsc] JWS: /demos/TopDown/ws/ComplexService_ComplexPortTypeSoapPortImpl.java Validated. [jwsc] Processing 1 JAX-WS web services... [jwsc] Compiling 1 source file to /tmp/_4ixn4v [jwsc] Building jar: /demos/TopDown/output/Complex_JAXWS_Ear/ws/ComplexService_ComplexPortTypeSoapPortImpl.war [jwsc] Created JWS deployment outputFile: /demos/TopDown/output/Complex_JAXWS_Ear/ws/ComplexService_ComplexPortTypeSoapPortImpl.war [jwsc] [EarFile] Application File : /demos/TopDown/output/Complex_JAXWS_Ear/META-INF/application.xml [AntUtil.deleteDir] Deleting directory /tmp/_4ixn4v deploy: [wldeploy] weblogic.Deployer -verbose -noexit -source /demos/TopDown/output/Complex_JAXWS_Ear -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -password ******** -deploy [wldeploy] weblogic.Deployer invoked with options: -verbose -noexit -source /demos/TopDown/output/Complex_JAXWS_Ear -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -deploy [wldeploy] <May 4, 2011 3:55:10 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, Complex_JAXWS_Ear [archive: /demos/TopDown/output/Complex_JAXWS_Ear], to AdminServer .> [wldeploy] Task 0 initiated: [Deployer:149026]deploy application Complex_JAXWS_Ear on AdminServer. [wldeploy] Task 0 completed: [Deployer:149026]deploy application Complex_JAXWS_Ear on AdminServer. [wldeploy] Target state: deploy completed on Server AdminServer [wldeploy] Target Assignments: [wldeploy] + Complex_JAXWS_Ear AdminServer BUILD SUCCESSFUL Total time: 5 seconds
Step8). Once the Service is deployed you can login to AdminConsole and then you can test it.
.
.
Thanks
Jay SenSharma