概念性小编
webservice目的:异构平台之间的交互
JAX-WS:Java Api XML Web Service(基于JAVAAPI XML实现的WebService)
SEI:Service Endpoint Interface(实例中的IMyService)
SIB:Service Implements Bean(实例中的MyServiceImpl)
一个简单的WebService实例
1 服务器的建立
1.1 创建接口
package com.lul.service; import javax.jws.WebService; @WebService() public interface IMyService { public int add(int a,int b); public int minus(int a,int b); }
1.2 创建实现类
package com.lul.service; import javax.jws.WebService; @WebService(endpointInterface="com.lul.service.IMyService") public class MyServiceImpl implements IMyService { @Override public int add(int a,int b) { System.out.println(a+"+"+b+"="+(a+b)); return (a+b); } @Override public int minus(int a,int b) { System.out.println(a+"-"+b+"="+(a-b)); return (a-b); } }
1.3 开启服务
package com.lul.service; import javax.xml.ws.Endpoint; public class MyService { public static void main(String[] args){ String address="http://localhost:8888/ns"; Endpoint.publish(address,new MyServiceImpl()); } }
启动成功后通过
2 客户端的建立
package com.lul.service; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class TestClient { public static void main(String[] args) { try { URL url=new URL("http://localhost:8888/ns?wsdl"); QName sname=new QName("http://service.lul.com/","MyServiceImplService"); Service service=Service.create(url,sname); IMyService ms=service.getPort(IMyService.class); System.out.println(ms.add(10,9)); } catch (MalformedURLException e) { // Todo Auto-generated catch block e.printstacktrace(); } } }运行结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。