- Tomcat
An open source servlet container. Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications, and provides a pure Java HTTP web server environment for Java code to run.
In Windows,
- Start Up Tomcat: By executing the following command: $CATALINA_HOME\bin\startup.bat. After startup, the default web applications included with Tomcat will be available by visiting: http://localhost:8888/. (We can change port 8080 to 8888 by editing $CATALINA_HOME\conf\server.xml. Port 8080 is reserved for Windows IIS.)
- Shut Down Tomcat: By executing the following command: $CATALINA_HOME\bin\shutdown.
- Start Up Tomcat: By executing the following command: $CATALINA_HOME\bin\startup.bat. After startup, the default web applications included with Tomcat will be available by visiting: http://localhost:8888/. (We can change port 8080 to 8888 by editing $CATALINA_HOME\conf\server.xml. Port 8080 is reserved for Windows IIS.)
- soapUI
soapUI is a free and open source cross-platform Functional Testing solution.
Creating a Web Service
- Setup Eclipse: Window > Preferences > Server > Runtime Environments > Add... > Apache Tomcat v7.0 > Next > Select correct Tomcat installation directory > Finish.
- New Project: File > New > Other... > Web > Dynamic Web Project > Target runtime: Apache Tomcat v7.0.
- New Java class:
package demo; public class HelloWorld { public String greeting(String name) { return "Hello World! " + name + "!"; } }
- Setup runtime environment: Right click on Servers tag below > New > Server. Select Tomcat v7.0 Server again.
- Create Web Service: Right click on HelloWorld.java in Project Explorer > Web Services > Create Web Service > Web service type: Bottom up Java bean Web Service. Service implementation: demo.HelloWorld. Click check box: Publish the Web service. > Finish.
or
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://demo" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://demo" xmlns:intf="http://demo" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://demo" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="greeting">
<complexType>
<sequence>
<element name="in0" type="xsd:string"/>
</sequence>
</complexType>
</element>
<element name="greetingResponse">
<complexType>
<sequence>
<element name="greetingReturn" type="xsd:string"/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name="greetingRequest">
<wsdl:part element="impl:greeting" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="greetingResponse">
<wsdl:part element="impl:greetingResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="greeting">
<wsdl:input message="impl:greetingRequest" name="greetingRequest">
</wsdl:input>
<wsdl:output message="impl:greetingResponse" name="greetingResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="greeting">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="greetingRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="greetingResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">
<wsdlsoap:address location="http://localhost:8888/WebServiceDemo/services/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
After linking to http://localhost:8888/WebServiceDemo/services/HelloWorld, we see:
<h1>HelloWorld</h1>
<p>Hi there, this is an AXIS service!</p>
<i>Perhaps there will be a form for invoking the service here...</i>
Testing a Web Service
We use soapUI 4.0.1 to test our web service. soapUI comes with extensive support for testing WSDL / SOAP based services (Tutorial).
- New Project: File > New soapUI Project > Initial WSDL/WADL: http://localhost:8888/WebServiceDemo/services/HelloWorld?wsdl
- Double click greeting in Navigator (left window). In XML tag in SOAP Request 1 (right window), we prepare the following request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:demo="http://demo"> <soapenv:Header/> <soapenv:Body> <demo:greeting> <demo:in0>Plover</demo:in0> </demo:greeting> </soapenv:Body> </soapenv:Envelope>
- Click Submit request to specified endpoint URL button. We will receive the following result:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <greetingResponse xmlns="http://demo"> <greetingReturn>Hello World! Plover!</greetingReturn> </greetingResponse> </soapenv:Body> </soapenv:Envelope>
Screenshot:
Enjoy!
沒有留言:
張貼留言