WEBSERVICE快速入门的示例:
首先定义接口:
package com.whaty.platform.ws.server; import javax.jws.WebService; /** * @className:IMyservice.java * @Desc:定义:SEI service endpoint interface * @author:lizhuang * @createTime:2012-12-21 上午12:57:18 */ //JAX-WS注解,表示java api xml for webservice。JDK自带API的XML格式的webservice @WebService public interface IMyservice { int add(int a, int b); int minus(int a, int b); }
package com.whaty.platform.ws.server; import javax.jws.WebService; /** * @className:MyServiceImpl.java * @Desc:定义:SIB service implemention bean * @author:lizhuang * @createTime:2012-12-21 上午01:01:22 */
//endpointInterface指定接入点接口:接口必须存在 @WebService(endpointInterface="com.whaty.platform.ws.server.IMyservice") public class MyServiceImpl implements IMyservice { public int add(int a, int b) { System.out.println("a+b="+(a+b)); return a+b; } public int minus(int a, int b) { System.out.println("a-b="+(a-b)); return a-b; } }
package com.whaty.platform.ws.server; import javax.xml.ws.Endpoint; /** * @className:MyServer.java * @Desc:发布服务 * @author:lizhuang * @createTime:2012-12-21 上午01:02:39 */ public class MyServer { public static void main(String[] args) { //访问方式:http://localhost:7777/tudou?wsdl String address="http://localhost:7777/tudou"; Endpoint.publish(address, new MyServiceImpl()); } }
http://localhost:7777/tudou?wsdl
浏览器显示如下:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.platform.whaty.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.platform.whaty.com/" name="MyServiceImplService"> <types> <xsd:schema> <xsd:import namespace="http://server.ws.platform.whaty.com/" schemaLocation="http://localhost:7777/tudou?xsd=1"/> </xsd:schema> </types> <message name="minus"> <part name="parameters" element="tns:minus"/> </message> <message name="minusResponse"> <part name="parameters" element="tns:minusResponse"/> </message> <message name="add"> <part name="parameters" element="tns:add"/> </message> <message name="addResponse"> <part name="parameters" element="tns:addResponse"/> </message> <portType name="IMyservice"> <operation name="minus"> <input message="tns:minus"/> <output message="tns:minusResponse"/> </operation> <operation name="add"> <input message="tns:add"/> <output message="tns:addResponse"/> </operation> </portType> <binding name="MyServiceImplPortBinding" type="tns:IMyservice"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="minus"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="add"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="MyServiceImplService"> <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"> <soap:address location="http://localhost:7777/tudou"/> </port> </service> </definitions>
下面我们创建客户端访问:
package com.whaty.platform.ws.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.whaty.platform.ws.server.IMyservice; /** * @className:MyClient.java * @Desc:访问发布的服务 * @author:lizhuang * @createTime:2012-12-21 上午01:23:57 */ public class MyClient { public static void main(String[] args) { try { //服务WSDL Document的地址 URL url = new URL("http://localhost:7777/tudou?wsdl"); //Qnameqname是qualified name 的简写 //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成 //由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/, QName qname=new QName("http://server.ws.platform.whaty.com/","MyServiceImplService"); Service service=Service.create(url, qname); IMyservice ms=service.getPort(IMyservice.class); ms.add(1, 4); ms.minus(1, 4); } catch (MalformedURLException e) { e.printStackTrace(); } } }
a+b=5
a-b=-3