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; } }
最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:
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 @R_293_4045@ion 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。