Sie sind auf Seite 1von 8

Creating SOA Services -----------------------------

The following is a Sample Code that will facilitate the creation of SOA Services.

Backend Code Creation and Deployment

1- Writing the Service Class Our services class is "HelloWorldService.java", let's create the same. Create a java file HelloWorldService.java into \HelloWorld\webservice\net\roseindiadirectory and add the code shown below. Here is the code of Hello World Webservice. package net.roseindia; public class HelloWorldService { public String sayHello(String name) { System.out.println("Hello World Service called"); return "Hello : " + name; } }

2- Web Services Descriptor Create services.xml in the META-INF directory. Create one or multiple services in the services.xml file (using the <service>...</service> tag) <service ><!-- Configuration of the service --></service> Here is the services.xml file for our application: <service><parameter name="ServiceClass"

locked="false">net.roseindia.HelloWorldService</parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </operation> </service> Our service class is net.roseindia.HelloWorldService and the message receiver isorg.apache.axis2.rpc.receivers.RPCMessageReceiver. The operation we are exposing is sayHello.

3- Compiling and building the service archieve Go to the source base directory for example above it will be: E:\Axis2Tutorial\Examples\HelloWorld\webservice>javac net/roseindia/*.java

4- Create the service achieve using the following command: E:\Axis2Tutorial\Examples\HelloWorld\webservice>jar cvf HelloWorldService.aar *

5- Deploying the Web Services Copy the HelloWorldService.aar into webapps/axis2/WEB-INF/services directory and restart the Tomcat. The Apache Axis2 engine will deploy the service on the serve

6- Verify services http://localhost:8080/axis2/services/listService

7- you can verify the WSDL You can view the WSDL file at http://localhost:8080/axis2/services/HelloWorldService?wsdl. Here is the WSDL file: <?xml version="1.0" encoding="UTF-8" ?>

- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:ns1="http://org. apache.axis2/xsd" xmlns:ns="http://roseindia.net"xmlns:wsaw="http://www.w3.org/2006/ 05/addressing/wsdl"xmlns:http="http://schemas.xmlsoap.org/wsdl/htt p/" xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:mime="http://s chemas.xmlsoap.org/wsdl/mime/"xmlns:soap="http://schemas.xmlsoa p.org/wsdl/soap/"xmlns:soap12="http://schemas.xmlsoap.org/wsdl/so ap12/" targetNamespace="http://roseindia.net"> - <wsdl:types> - <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://roseindia.net"> - <xs:element name="sayHello"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="args0" nillable="true"type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="sayHelloResponse"> - <xs:complexType> - <xs:sequence> <xs:element minOccurs="0" name="return"nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> - <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="ns:sayHello" /> </wsdl:message> - <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="ns:sayHelloResponse" /> </wsdl:message> - <wsdl:portType name="HelloWorldServicePortType"> - <wsdl:operation name="sayHello"> <wsdl:input message="ns:sayHelloRequest"wsaw:Action="urn:sayHello" /> <wsdl:output message="ns:sayHelloResponse"wsaw:Action="urn:sayHelloResponse" /> </wsdl:operation>

</wsdl:portType> - <wsdl:binding name="HelloWorldServiceSoap11Binding"type="ns:HelloWorldServic ePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"style="document" / > - <wsdl:operation name="sayHello"> <soap:operation soapAction="urn:sayHello" style="document" /> - <wsdl:input> <soap:body use="literal" /> </wsdl:input> - <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="HelloWorldServiceSoap12Binding"type="ns:HelloWorldServic ePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"style="document" / > - <wsdl:operation name="sayHello"> <soap12:operation soapAction="urn:sayHello" style="document" /> - <wsdl:input> <soap12:body use="literal" /> </wsdl:input> - <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:binding name="HelloWorldServiceHttpBinding"type="ns:HelloWorldServicePo rtType"> <http:binding verb="POST" /> - <wsdl:operation name="sayHello"> <http:operation location="HelloWorldService/sayHello" /> - <wsdl:input> <mime:content type="text/xml" part="sayHello" /> </wsdl:input> - <wsdl:output> <mime:content type="text/xml" part="sayHello" /> </wsdl:output> </wsdl:operation>

</wsdl:binding> - <wsdl:service name="HelloWorldService"> - <wsdl:port name="HelloWorldServiceHttpSoap11Endpoint" binding="ns:HelloWorldServiceSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/HelloWorldService .HelloWorldServiceHttpSoap11Endpoint/" /> </wsdl:port> - <wsdl:port name="HelloWorldServiceHttpSoap12Endpoint"binding="ns:HelloWorl dServiceSoap12Binding"> <soap12:addresslocation="http://localhost:8080/axis2/services/HelloW orldService .HelloWorldServiceHttpSoap12Endpoint/" /> </wsdl:port> - <wsdl:port name="HelloWorldServiceHttpEndpoint"binding="ns:HelloWorldServic eHttpBinding"> <http:addresslocation="http://localhost:8080/axis2/services/HelloWorl dService .HelloWorldServiceHttpEndpoint/" /> </wsdl:port> </wsdl:service> </wsdl:definitions> Client End Creation and Deployment 1- Generating the client code using wsdl2java.bat tool Now create a new directory E:\Axis2Tutorial\Examples\HelloWorld\client and then open dos prompt and go to same directory. Now generate the client code using following command: WSDL2Java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -o client Here is the output of above command.E:\>cd E:\Axis2Tutorial\Examples\HelloWorld\client E:\Axis2Tutorial\Examples\HelloWorld\client>WSDL2Java.bat -uri http://localhost:8080/axis2/services/HelloWorldService?wsdl -o clientUsing

AXIS2_HOME: E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1Using JAVA_HOME: E:\JDK\jdk1.6.0_03Retrieving document at 'http://localhost:8080/axis2/services/HelloWorldService?wsdl'. The above command will generate a) HelloWorldServiceStub.java and b) HelloWorldServiceCallbackHandler.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory. Now run cd to client/src directory. cd client/src Developing the code to call the service Create the file Test.java into E:\Axis2Tutorial\Examples\HelloWorld\client\client\src\net\roseindia directory. The code of Test.java file is as below:

package net.roseindia; import net.roseindia.*; import net.roseindia.HelloWorldServiceStub.SayHello; public class Test { public static void main(String[] args) throws Exception { HelloWorldServiceStub stub = new HelloWorldServic eStub(); //Create the request net.roseindia.HelloWorldServiceStub.SayHello request = new net.roseindia.HelloWorldServiceStub.SayHello(); request.setArgs0("Deepak Kumar"); //Invoke the service net.roseindia.HelloWorldServiceStub.SayHelloRespons e response = stub.sayHello(request); System.out.println("Response : " + response.get_ret urn()); } } Compiling and testing the Web service

Now go to E:\Axis2Tutorial\Examples\HelloWorld\client\client\src directory and with the help of javac command compile the code. E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.javaNote: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details. To run the client type following command:java net/roseindia/Test Here is the output: E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>javac net/roseindia/*.javaNote: net\roseindia\HelloWorldServiceStub.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.E:\Axis2Tutorial\Examples\HelloWorld\client\client\src>java net/roseindia/Testlog4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).log4j:WARN Please initialize the log4j system properly.Response : Hello : Deepak Kumar E:\Axis2Tutorial\Examples\HelloWorld\client\client\src> The client appliction makes a call to Web service and in response Web services returns theHello : Deepak Kumar message You have successfully tested your Web Service.

Writing the build.xml file Now we will see how we can develop the code for ant build. I am assuming that you have installed Axis2 binary at E:\Axis2Tutorial\axis2-1.5.1bin\axis2-1.5.1 and the url of WSDL file is http://localhost:8080/axis2/services/HelloWorldService?wsdl. You can update the build.xml file to point to the axis2 location directory in your computer. Here is the code of build.xml file. <?xml version="1.0" ?> - <project name="antwsdl2java" default="gen" basedir="."> - <path id="axis2.classpath">

- <fileset dir="E:\Axis2Tutorial\axis2-1.5.1-bin\axis2-1.5.1"> <include name="**/*.jar" /> </fileset> </path> - <target name="gen"> <taskdef name="axis2wsdl2java"classname="org.apache.axis2.tool.ant.AntCodegenTask"cla sspathref="axis2.classpath" /> <axis2wsdl2javawsdlfilename="http://localhost:8080/axis2/services/HelloWorl dService?wsdl" output="src/services" /> </target> </project>

Das könnte Ihnen auch gefallen